Java实验2 类和对象

实验目的

  1. 掌握类和对象的创建和使用;
  2. 掌握类的封装性;
  3. 掌握构造方法的定义和重载;
  4. 掌握this和static关键字的使用。
    实验内容
    1.写一个名为Rectangle的类表示矩形。其属性包括宽width、高height和颜色color,width和height都是double型的,而color则是String类型的。要求该类具有:
    (1)使用构造函数完成各属性的初始赋值。
package Rectangle矩形;
public class Rectangle {
	private double width;
	void i(){
		System.out.println("面积为"+width*height);
	}
	public double getWidth(){
		return width;
	}
	public void setProwidth(double width){
		this.width=width;
	}
	
	private double height;
	public double getHeight(){
		return height;
	}
	public void setProheight(double heihgt){
		this.height=heihgt;
	}
	
	public  Rectangle(double width,double height)
	{
		this.width=width;
		this.height=height;
	}
}

(2)使用getter和setter的形式完成属性的访问及修改。

package Rectangle矩形;

public class Color {
	private String color;
	private String getColor(){
		return color;
	}
	public String setProcolor(String color){
		return this.color=color;
	}
	public Color(String color) {
		this.color=color;
	}
	void p(){
		System.out.print("颜色为"+color);
	}
}

(3)提供计算面积的getArea()方法。
package Rectangle矩形;

public class mainRectangle {

	public static void main(String[] args) {
		Rectangle p1=new Rectangle(2.2, 3.3);
		p1.i();
		Color p2=new Color("蓝色");
		p2.p();
	}

}

2.写出下面的程序运行结果,并解释原因。


```java
 public class My {
      String s;
      public void My(){
        s = "Constructor";
       }
      public void go() {
        System.out.println(s);
      }
      public static void main(String args[]) {
        My m = new My();
        m.go();
      }
  }

noll
返回值为空

3.定义一个Teacher类,该类提供教师的工号、姓名、职称信息,再定义一个课程类,描述了课程的编号、课程名称、理论课时和实验课时、学分以及每门课程都要指定一个教师,通过课程能够获得任课教师对象。

package teacher;
public class Teacher {
	public String name;
	public Teacher() {
	} 
	public Teacher(String name) {
		this.name=name;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}
package teacher;

public class Course {
	private String cname;
	private Teacher t;
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public Teacher getT() {
		return t;
	}
	public void setT(Teacher t) {
		this.t = t;
	}
	public Course(String cname, Teacher t) {
		super();
		this.cname = cname;
		this.t = t;
	}	
	public Course() {
		
	}
}
package teacher;

import ch2.ch2;

public class Test {

	public static void main(String[] args) {
		Course c1=new Course("java",new Teacher("liu"));
		Course c2=new Course();
		c2.setCname("shujujiegou");
		Teacher t1=new Teacher("luan");
		c2.setT(t1);
		System.out.println("gai ke cheng de ming cheng wei:"+c1.getCname());
		System.out.println(c1.getT().getName());
	}

}

4.定义满足以下条件的Student类,并创建对象对其进行测试。
(1)Student类的属性:姓名、年龄、班级、学校、期末考试总分数、考试科目数量。

package Student;
public class Student {
	void speak(){
		System.out.println("我叫"+name+",今年"+age+"岁!"+"是"+school+Class+"班的学生。");
	}
	private String name;
	public String getName(){
		return name;
	}
	public void setProname(String name){
		this.name=name;
	}
	private int age;
	public int getAge() {
		return age;
	}
	public void setProage(int age) {
		this.age=age;
	}
	private int Class;
	public int getclass() {
		return Class;
	}
	public void setProclass(int Class){
		this.Class=Class;
	}
	private String school;
	public String getSchool(){
		return school;
	}
	public void setProschool(String school) {
		this.school=school;
	}
	private int fraction;
	public int getFraction(){
		return fraction;
	}
	public void setProfraction(int fraction) {
		this.fraction=fraction;
	}
	private int number;
	public int getNumber(){
		return number;
	}
	public void setPronumber(int number) {
		this.number=number;
	}
}

(2)Student类的方法:自我介绍、输出考试平均分。

package Student;
public class main {

	public static void main(String[] args) {
		Student p1=new Student();
		p1.setProname("张三");
		p1.setProage(18);
		p1.setProclass(6);
		p1.setProschool("枣庄学院");
		p1.setProfraction(550);
		p1.setPronumber(6);
		p1.speak();
		double average=p1.getFraction()/p1.getNumber();
		System.out.print("我的平均分为"+average+"分。");
	}
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值