Java基础部分题(参考答案)

发布时间 : 星期三 文章Java基础部分题(参考答案)更新完毕开始阅读

}

public static void main(String args[]) { ComplexNumber cn1 = new ComplexNumber(2,4); ComplexNumber cn2 = new ComplexNumber(1,-1); ComplexNumber cn3; cn3 = cn1.complexMulti(cn2); System.out.println(cn1.getRealPart()+\ System.out.println(cn2.getRealPart()+\ System.out.println(cn3.getRealPart()+\}

8. //类的定义

写一个名为Rectangle的类表示矩形。属性为宽width、高height和颜色color,widthhe height是double型的,color是String型的。假定所有矩形颜色相同,用一个类变量(类属性)表示颜色。要求提供访问器方法和计算矩形面积的findArea()方法。

写一个用户程序测试Rectangle类:创建两个Rectangle对象。给两个对象设置任意的宽和高。设矩形颜色为黄色。显示两个对象的属性并求面积。 答案:

public class Rectangle { private double width = 1; private double height = 1; private static String color = “white”; public Rectangle() { } public Rectangle(double width, double height , String color) { this.width = width; this.height = height; this.color = color; } public double getWidth() { return width; }

public void setWidth(double width) { this.width = width; }

public double getHeight() { return height; }

public void setHeight(double height) { this. height = height; }

public static String getColor() { return color; }

public static void setColor(String color) { this.color = color; }

public double findArea() { return width*height; }

public static void main(String[] args) { Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle();

r1.setWidth(2.0); r1.setHeight(3.0); r1.setColor(“红色”); r2.setWidth(5.0); r2.setHeight(4.0); r2.setColor(“黄色”);

System.out.println(r1.getWidth()); System.out.println(r1.getHeight());

}

}

System.out.println(r1.getColor()); System.out.println(r1.findArea()); System.out.println(r2.getWidth()); System.out.println(r2.getHeight()); System.out.println(r2.getColor()); System.out.println(r2.findArea());

9、//static修饰符

设计一个学生类,其中成员变量应包括:学号,姓名,性别,班级,并提供方法打印学生信息,和计算已经创建学生对象的数目。编写一个程序测试这个类。 public class Student {

private long id;

private String name; private char sex; private String banji;

private static int count=0;

public Student(long id,String name,char sex,String banji) {

this.id=id;

this.name=name; this.sex=sex;

this.banji=banji; count++; }

public void print() {

System.out.println(“该生信息如下:”);

System.out.println(“学号:”+id+” 姓名:”+name+” 性别:”+sex+” 班级:”+banji);

}

public static void count() {

System.out.println(“已经创建的学生个数为:”+count);

}

public static void main(String[] args) {

Student a=new Student(01,“zhangsan”,?男?,”计专01班”); a.print();

Student.count();

Student b=new Student(03,”刘倩”,?女?,”计专03班”); b.print();

Student.count();

} }

10、//static修饰符

编写一个类Teacher,描述教师的课时数量和计算课时的系数,均为double类型。Teacher类还有一个方法,courseCompute(),可计算教师的当量课时(课时量*系数),返回值类型为double,所有教师的课时系数相同。 编写一个测试类进行测试。

创建两个教师对象,计算的系数为1.2,输出计算后的两位老师的当量课时。 将系数修改后,输出修改后的当量课时。 public class Test{ public static void main(String[] args){ Teacher t1=new Teacher(96); Teacher t2=new Teacher(64); Teacher.classXishu=1.2; double realCourseNum=t1.courseCompute();

System.out.println(\第一位老师的实际课时数为:\realCourseNum=t2.courseCompute();

System.out.println(\第一位老师的实际课时数为:\Teacher.classXishu=1.5;

realCourseNum=t1.courseCompute();

System.out.println(\更改系数后,第一位老师的实际课时数为:\ realCourseNum=t2.courseCompute(); System.out.println(\更改系数后,第一位老师的实际课时数为:\ }

联系合同范文客服:xxxxx#qq.com(#替换为@)