Java实验3(2)

2019-04-22 21:16

// Setter method for width

public void setWidth(double width) {

this.width = width; }

// Getter method for height public double getHeight() {

return height; }

// Setter method for height

public void setHeight(double height) {

this.height = height; }

// Implement the findArea method in GeometricObject public double findArea() {

return width*height; }

// Implement the findPerimeter method in GeometricObject public double findPerimeter() {

return 2*(width + height); }

// Override the equals() method defined in the Object class public boolean equals(Rectangle rectangle) {

return (width == rectangle.getWidth()) && (height == rectangle.getHeight()); }

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

return \ } }

4、使用Windows写字板编辑类TestPolymorphism,源代码如下: public class TestPolymorphism {

// Main method

public static void main(String[] args) {

// Declare and initialize two geometric objects GeometricObject geoObject1 = new Circle(5);

GeometricObject geoObject2 = new Rectangle(5, 3);

System.out.println(\ equalArea(geoObject1, geoObject2));

// Display circle

displayGeometricObject(geoObject1);

// Display rectangle

displayGeometricObject(geoObject2); }

// A method for comparing the areas of two geometric objects static boolean equalArea(GeometricObject object1, GeometricObject object2) {

return object1.findArea() == object2.findArea(); }

// A method for displaying a geometric object

static void displayGeometricObject(GeometricObject object) {

System.out.println();

System.out.println(object.toString());

System.out.println(\

System.out.println(\ } }

5、把上面编辑的几个源程序保存成Java源程序文件(扩展名为java),程序

文件名分别为GeometricObject.java、Circle.java、Rectangle.java、TestPolymorphism.java。

6、进入命令提示符状态,在源程序文件存放目录下对以上文件进行编译。

观察编译后源程序文件存放目录中的文件,熟悉Java程序的文件结构。 7、如果编译正确,则键入如下命令行,使用Java解释器运行源程序:

java TestPolymorphism

程序运行结果如下:

8、使用Windows写字板编辑编辑类TestInterface源程序如下: public class TestInterface {

// Main method

public static void main(String[] args) {

// Create two comarable circles

ComparableCircle circle1 = new ComparableCircle(5); ComparableCircle circle2 = new ComparableCircle(4);

// Display the max circle

Comparable circle = Max.max(circle1, circle2); System.out.println(\ ((Circle)circle).getRadius()); System.out.println(circle);

// Create two comarable cylinders

ComparableCylinder cylinder1 = new ComparableCylinder(5, 2); ComparableCylinder cylinder2 = new ComparableCylinder(4, 5);

// Display the max cylinder

Comparable cylinder = Max.max(cylinder1, cylinder2); System.out.println();

System.out.println(\ cylinder1.findVolume());

System.out.println(\ cylinder2.findVolume());

System.out.println(\

((Cylinder)cylinder).getRadius() + \ ((Cylinder)cylinder).getLength() + \ ((Cylinder)cylinder).findVolume()); System.out.println(cylinder); } }

// ComparableCircle is a subclass of Circle, which implements the // Comparable interface

class ComparableCircle extends Circle implements Comparable {

// Construct a CompareCircle with specified radius public ComparableCircle(double r) {

super(r); }

// Implement the compareTo method defined in Comparable public int compareTo(Object o) {

if (getRadius() > ((Circle)o).getRadius()) return 1;

else if (getRadius() < ((Circle)o).getRadius()) return -1; else

return 0; } }

// ComparableCylinder is a subclass of Cylinder, which implements the // CompareObject interface

class ComparableCylinder extends Cylinder implements Comparable {

// Construct a CompareCylinder with radius and length ComparableCylinder(double r, double l) {

super(r, l); }

// Implement the compareTo method defined in Comparable interface public int compareTo(Object o) {

if (findVolume() > ((Cylinder)o).findVolume()) return 1;

else if (findVolume() < ((Cylinder)o).findVolume()) return -1; else

return 0; } }

10、使用Windows写字板编辑编辑类Circle源程序如下: public class Circle {

protected double radius;

public Circle() {

radius=1.0; }

// Construct circle with specified radius public Circle(double r) {

radius=r; }

// Getter method for radius public double getRadius() {

return radius; }

// Setter method for radius

public void setRadius(double radius) {

this.radius = radius; }

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

return radius*radius*Math.PI; }

public boolean equals(Circle circle) {

return this.radius == circle.getRadius(); }

// Override the toString() method defined in the Object class


Java实验3(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:取消学校、医院行政级别有利于公共服务均衡化

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

马上注册会员

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