收藏 分销(赏)

2023年面向对象程序设计试题.doc

上传人:w****g 文档编号:7150084 上传时间:2024-12-27 格式:DOC 页数:9 大小:73.04KB 下载积分:6 金币
下载 相关 举报
2023年面向对象程序设计试题.doc_第1页
第1页 / 共9页
2023年面向对象程序设计试题.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
得分 一、程序修改(20分) 说明:描述程序中的语法错误因素并修改,每小题4分,错误因素和改正错误各2分。 1. isPrime方法用于检查参数num是否是质数。 public boolean isPrime(int num){ boolean result = false; for(int i=2; i<num; i++){ if(num%i==0) break; } if(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 double[num]; Scanner 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]; } 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(); scores[i] = inputScore( (3) ); } for (int i = 0; i < numOfClass; i++) { System.out.print("第" + (i + 1) + "班的总分:"); System.out.println(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) //把参数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 { 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. 阅读程序1 class 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("array[0]=" + array[0] + ",array[1]=" + array[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.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; } } 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.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. 阅读程序3 class 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(value<40){ 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.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 radius) { 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; } 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.java public 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 (shapes[i] instanceof CalcArea) { // result += ((CalcArea) shapes[i]).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分)
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服