1、得分
一、程序修改(20分)
阐明:描述程序中的语法错误原因并修改,每题4分,错误原因和更正错误各2分。
1. isPrime措施用于检查参数num是否是质数。
public boolean isPrime(int num){
boolean result = false;
for(int i=2; i
2、ble { void read(); } class Reader implements Readable { void read() { System.out.println("I Can read it."); } } 3. main措施调用重载的max措施求2个数的最大值。 public class Error03 { public static double max(int a, double b) { return a > b ? a : b; } public static double max(double a, int b) { return a >
3、b ? a : b; } public static void main(String[] args) { double c = max(10, 100); } } 4. 子类Child覆盖了父类Parent中的output措施。 class Parent{ public final void output() { System.out.println("Parent"); } } class Child extends Parent{ public void output() { System.out.println("Child"); } } 5.
4、main措施调用sum措施求数组所有元素的和。 public class Error05 { public double sum(double[] array) { double result = 0; for (double value : array) { result += value; } return result; } public static void main(String[] args) { double[] arr = {1.0, 2.0, 3.0, 4.0, 5.0}; System.out.pr
5、intln(sum(arr)); } } 得分 二、程序填空(20分) 阐明:填充程序中的空白,使程序能够正确运行,每空2分。 1. 如下程序功效为输入多个班的考试成绩,并分别计算每个班的总成绩。 import java.util.Scanner; public class Score { //inputScore措施用于输入一个班的所有成绩,参数num是班级的学生人数 public static double[] inputScore(int num) { double[] array = new double[num]; Sca
6、nner scanner = new Scanner(System.in); for (int i = 0; i < (1) ; i++) { array[i] = scanner.nextDouble(); } return array; } public static double sumScore(double[] array) { double result = 0; for (int i = 0; i < array.length; i++) { result += array[i
7、]; } return result; } public static void main(String[] args) { double[][] scores; int numOfClass, numOfStudent; Scanner scanner = new Scanner(System.in); System.out.print("一共有几个班?"); numOfClass = scanner.nextInt(); scores = (2) ;
8、for (int i = 0; i < numOfClass; i++) { System.out.println("第" + (i + 1) + "班有几人?"); numOfStudent = scanner.nextInt(); scores[i] = inputScore( (3) ); } for (int i = 0; i < numOfClass; i++) { System.out.print("第" + (i + 1) + "班的总分:"); System.out.p
9、rintln(sumScore( (4) )); } } } 2. 如下程序定义了Circle和Cylinder两个类。 //类1,Circle.java public class Circle { private double radius; //圆的半径 public Circle() { //无参结构措施 (5) //调用有参结构措施将radius初始化为0.0 } public Circle(double radius) { (6)
10、 //把参数radius赋给数据域radius } public double getArea(){ return (7) ; //求圆的面积,使用Math.PI } public double getRadius() { return radius; } public void setRadius(double r) { radius = r; } } //类2,Cylinder.java public class Cylinder extends Circle {
11、private double height; //圆柱的高度 public Cylinder() { //无参结构措施 this.height = 0.0; } public Cylinder(double radius, double height) { (8) ; //调用父类结构措施将radius初始化为参数radius this.height = height; } @Override public double getArea() { double area1 =
12、 (9) ; //求圆柱表面积 double area2 = (10) ; //求圆柱的底面积 return area1 + area2; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } } 得分 三、阅读程序(20分) 阐明:阅读如下每段程序,写出运行的成果,每题5分。 1. 阅读程序1 class Data {
13、 public int a = 10, b = 100; } public class Read01 { public static void main(String[] args) { int a = 10, b = 100; int[] array = {10, 100}; Data data = new Data(); System.out.println("a=" + a + ",b=" + b); System.out.println("array[0]=" + array[0] + ",array[1]=" + arra
14、y[1]); System.out.println("data.a=" + data.a + ",data.b=" + data.b); swap(a, b); swap(array); swap(data); System.out.println("a=" + a + ",b=" + b); System.out.println("array[0]=" + array[0] + ",array[1]=" + array[1]); System.out.println("data.a=" + data.a + ",data.
15、b=" + data.b); } public static void swap(int a, int b) { int t = a; a = b; b = t; } public static void swap(int[] array) { int t = array[0]; array[0] = array[1]; array[1] = t; } public static void swap(Data data) { int t = data.a; data.a = data.b; data.b = t
16、 } } 2. 阅读程序2 public class Read02 { public static void main(String[] args) { A x = new B(); System.out.println("(1)x.i: "+x.i); System.out.println("(2)(B)x.i: "+((B)x).i); System.out.println("(3)x.j: "+x.j); System.out.println("(4)(B)x.j: "+((B)x).j); System.o
17、ut.println("(5)x.m1(): "+x.m1()); System.out.println("(6)(B)x.m1(): "+((B)x).m1()); System.out.println("(7)x.m2(): "+x.m2()); System.out.println("(8)x.m3(): "+x.m3()); } } class A { public int i=1; public static int j=11; public static String m1() { return "类A的静态措施m1.";}
18、 public String m2() { return "类A的实例措施m2.";} public String m3() { return "类A的实例措施m3.";} } class B extends A{ public int i=2; public static int j=22; public static String m1() { return "类B的静态措施m1.";} public String m2() { return "类B的实例措施m2.";} } 3. 阅读程序3 class Person { public
19、Person() { System.out.println("Person()"); } } class Employee extends Person { public Employee() { this("调用Employee(s)"); System.out.println("Employee()"); } public Employee(String s) { System.out.println(s); } } class Faculty extends Employee{ public Faculty(){
20、 System.out.println("Faculty()"); } } class Test{ public static void main(String[] args) { new Faculty(); } } 4. 阅读程序4, 分别写出??处的值是30和50的输出成果。 public class Read04 { public static void main(String[] args) { int value = ??; try { if(value<40){ throw new Excepti
21、on("value is too small"); } System.out.println("value="+value); } catch (Exception e) { System.out.println(e.getMessage()); } finally{ System.out.println("process finished"); } System.out.println("program continued"); } } 四、程序设计(40分) 1. 设计并编写一
22、个名为MyPoint的类表示平面上一个具备x坐标和y坐标的点,完成如下要求:(15分) l 将该类放置于包prog01中; l 两个double类型数据域x和y表示坐标,并进行封装; l 无参结构措施创建点(0.0, 0.0); 有参结构措施按指定坐标创建一个点; l 措施distance返回目前点对象到参数点对象之间的距离; l 编写测试类TestMyPoint,其main措施中创建两个点(0.0, 0.0)和(10.0, 35.5),输出这两个点之间的距离。 2.依照材料完成程序代码(15分)。 要求利用多态性计算若干不一样类型几何图形的面积之和,类与接口的关系见下面的类
23、图。类CricleV1,类RectangleV1和测试类Tester的源码已经给出。 CircleV1.java public class CircleV1 { private double radius; public CircleV1() { this(1.0); } public CircleV1(double radius) { this.radius = radius; } public double getRadius() { return radius; } public void setRadius(double r
24、adius) { this.radius = radius; } } RectangleV1.java public class RectangleV1 { private double width; private double height; public RectangleV1() { this(1.0, 1.0); } public RectangleV1(double width, double height) { this.width = width; this.height = height; } publ
25、ic 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; } } Tester.java public class Tester { public static void main(Str
26、ing[] args) { Object[] shapes = { new CircleV2(10), // new RectangleV2(10, 2), // new CircleV2(), // new RectangleV2() // }; System.out.println(sumArea(shapes)); } public static double sumArea(Object[] shapes) { double res
27、ult = 0; for (int i = 0; i < shapes.length; i++) { if (shapes[i] instanceof CalcArea) { // result += ((CalcArea) shapes[i]).getArea(); // } } return result; } } 请依照以上给定的材料,完成如下代码,使的Tester类中的main措施能够顺利运行。注意Tester类中行尾有“//”标注的行使用了你需要完成的接口和类。 (1)
28、编写完成接口CalcArea.java。(5分) (2) 编写完成类CircleV2.java。(5分) (3) 编写完成类RectangleV2.java。(5分) 3. 依照材料完成程序代码(15分)。 import java.util.Date; public class Account { private int id; //账号 private double balance; //账户余额 private Date createDate;//开户日期 public Account(int id, double balance) {
29、 this.id = id; this.balance = balance; this.createDate = new Date(); } //取款措施 public void getMoney(double amt){ } //如下是3个访问器措施 public int getId() { return id; } public double getBalance() { return balance; } public Date getCreateDate() { return createDate; } } 依照以上给出的银行账户类Account的定义,按要求完成如下代码。阐明:答卷上只填写修改部分的代码即可,原有代码的其他部分不需要重写。 (1) 修改Account类的定义,使得Account类能够进行序列化。(3分) (2) 修改并完成getMoney措施,要求:假如余额足够,则减去参数amt给出的金额;否则getMoney措施抛出一个Exception异常对象,异常信息为“余额不足.”。(3分) (3) 修改Account类的定义,使得Account类能够进行“深克隆”,阐明:Date类能够进行克隆。(4分)






