资源描述
装订线得分一、程序修改(20分)说明:描述程序中的语法错误原因并修改,每小题4分,错误原因和改正错误各2分。1. isPrime方法用于检查参数num是否是质数。public boolean isPrime(int num) boolean result = false; for(int i=2; i=num) result = true; return result;2. 具体类Reader实现了Readable接口。interface Readable 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 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. 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.println(sum(arr); 得分二、程序填空(20分)说明:填充程序中的空白,使程序能够正确运行,每空2分。1. 以下程序功能为输入多个班的考试成绩,并分别计算每个班的总成绩。import java.util.Scanner;public class Score /inputScore方法用于输入一个班的所有成绩,参数num是班级的学生人数 public static double inputScore(int num) double array = new doublenum; Scanner scanner = new Scanner(System.in); for (int i = 0; i (1) ; i+) arrayi = scanner.nextDouble(); return array; public static double sumScore(double array) double result = 0; for (int i = 0; i array.length; i+) result += arrayi; 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) ; for (int i = 0; i numOfClass; i+) System.out.println(第 + (i + 1) + 班有几人?); numOfStudent = scanner.nextInt(); scoresi = inputScore( (3) ); for (int i = 0; i numOfClass; i+) System.out.print(第 + (i + 1) + 班的总分:); System.out.println(sumScore( (4) ); 2. 以下程序定义了Circle和Cylinder两个类。/类1,Circle.javapublic class Circle private double radius; /圆的半径 public Circle() /无参构造方法 (5) /调用有参构造方法将radius初始化为0.0 public Circle(double radius) (6) /把参数radius赋给数据域radius public double getArea() return (7) ; /求圆的面积,使用Math.PI public double getRadius() return radius; public void setRadius(double r) radius = r; /类2,Cylinder.javapublic class Cylinder extends Circle 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 = (9) ; /求圆柱表面积double area2 = (10) ; /求圆柱的底面积return area1 + area2; public double getHeight() return height; public void setHeight(double height) this.height = height; 得分三、阅读程序(20分)说明:阅读以下每段程序,写出运行的结果,每小题5分。1. 阅读程序1class Data 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(array0= + array0 + ,array1= + array1); 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(array0= + array0 + ,array1= + array1); System.out.println(data.a= + data.a + ,data.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 = array0; array0 = array1; array1 = t; public static void swap(Data data) int t = data.a; data.a = data.b; data.b = t; 2. 阅读程序2public 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.out.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.; 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. 阅读程序3class Person public 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() 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(value40) throw new Exception(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. 设计并编写一个名为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分)。要求利用多态性计算若干不同类型几何图形的面积之和,类与接口的关系见下面的类图。类CricleV1,类RectangleV1和测试类Tester的源码已经给出。CircleV1.javapublic 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 radius) this.radius = radius; RectangleV1.javapublic 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; 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; Tester.javapublic class Tester public static void main(String 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 result = 0; for (int i = 0; i shapes.length; i+) if (shapesi instanceof CalcArea) / result += (CalcArea) shapesi).getArea(); / return result; 请根据以上给定的材料,完成以下代码,使的Tester类中的main方法能够顺利运行。注意Tester类中行尾有“/”标注的行使用了你需要完成的接口和类。(1) 编写完成接口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) 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分)第 7 页 共 8 页
展开阅读全文