实验四 抽象类、接口和图形

2019-08-30 20:18

实验四 抽象类、接口和图形

(上课被教师验收的同学不用提交实验结果,并注意在教师处签名。未完成的同学请提交实验结果,即将程序直接粘贴至本DOC文件,并在邮件名的标题中注明:学号+姓名+实验三)

1. 给出下面程序的输出:

Value is too small

Continue after the catch block

Continue after the catch block

2. 假设下面的try-catch块中的statement2引起一个异常:

No No Yes

No

3. 在下面的定义中,哪个定义了一个合法的抽象类? 4.

C

4.为什么下面两行代码可以编译,但会导致运行时错误?

Number numbeRef = new Integer(0); Double doubleRef = (Double)numberRef;

5. 下面的说法为真还是为假?

(1) 除了不能使用new操作符创建抽象类的实例之外,一个抽象类可以像非抽象类一样使

用。 T (2) clone()为Object的protected方法,可以在Object的子类当中使用。

(3) 可以将new int[10]、new String[100]、new Object[50]或者new Calendar[20]赋值给一个

Object[]类型的变量。

(4) 抽象类可以使用该抽象类的构造方法创建实例。 (5) 接口被编译成独立的字节码文件。 (6) 非抽象父类的子类不能是抽象类。

6. 编写一个方法,返回对象数组中最大的对象。方法签名如下:

public static Object max(Comparable[] a)

所有对象都是Comparable接口的实例。对象在数组中的顺序是由CompareTo方法决定的。

编写测试程序,创建一个由10个字符串构成的数组、一个由10个整数构成的数组和一个由10个日期构成的数组,找出数组中最大的字符串、整数和日期。

7. 创建一个自定制面板,它可以显示X、O或者空白。显示什么是重画面板时随时决定的。

使用Math.random()方法随机产生整数0、1或2,对应于面板上显示X、0或者空白。创建一个包含9个自定制面板的框架,如图15-29b所示。

8. 编写一个方法,求数组中所有几何对象的面积之和。方法签名如下:

public static double sumArea(GeometricObject[] a)

编写测试程序,创建四个对象(两个圆和两个矩形)的数组,然后使用sumArea方法求它们的总面积。

(GeometricObject class和Circle class在此提供,请自拟Rectangle类) // Circle.java: The circle class that extends GeometricObject public class Circle extends GeometricObject { private double radius;

/**Default constructor*/ public Circle() { this(1.0); }

/**Construct circle with a specified radius*/ public Circle(double radius) { this(radius, \ }

/**Construct a circle with specified radius, filled, and color*/ public Circle(double radius, String color, boolean filled) { super(color, filled); this.radius = radius; }

/**Return radius*/

public double getRadius() { return radius; }

/**Set a new radius*/

public void setRadius(double radius) { this.radius = radius; }

/**Implement the getArea method defined in GeometricObject*/ public double getArea() {

return radius*radius*Math.PI; }

/**Implement the getPerimeter method defined in GeometricObject*/ public double getPerimeter() { return 2*radius*Math.PI; }

/**Override the equals() method defined in the Object class*/ public boolean equals(Circle circle) { return this.radius == circle.getRadius(); }

/**Override the toString() method defined in the Object class*/ public String toString() {

return \

}

}

// GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = \white\ private boolean filled;

/**Default construct*/ protected GeometricObject() { }

/**Construct a geometric object*/

protected GeometricObject(String color, boolean filled) { this.color = color; this.filled = filled; }

/**Getter method for color*/ public String getColor() { return color; }

/**Setter method for color*/ public void setColor(String color) { this.color = color;

}

/**Getter method for filled. Since filled is boolean, so, the get method name is isFilled*/ public boolean isFilled() { return filled; }

/**Setter method for filled*/ public void setFilled(boolean filled) { this.filled = filled; }

/**Abstract method findArea*/ public abstract double getArea();

/**Abstract method getPerimeter*/

public abstract double getPerimeter(); }


实验四 抽象类、接口和图形.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:二次开挖箱涵专项施工方案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: