Java 代码作业

一:题目

你好,我是悦创。

在这里插入图片描述

定义三个类:

  1. 父类 GeometricObject 代表几何形状
  2. 子类 Circle 代表圆形,MyRectangle 代表矩形
  3. 定义一个测试类 GeometricTest,编写 equalsArea 方法测试两个对象的面积是否相等(注意方法的参数类型,利用动态绑定技术),编写 displayGeometricObject 方法显示对象的面积
  4. 注意方法的参数类型,利用动态绑定技术。

二:代码如下:

1. 父类:GeometricObject

// project = 'GeometricObject', file_name = 'GeometricObject', author = 'AI悦创(黄家宝)'
// time = '2020/3/29 13:31', product_name = Sublime Text3
// # code is far away from bugs with the god animal protecting
//     I love animals. They taste delicious.

public class GeometricObject {

    protected double weight;
    protected String color;
    public GeometricObject(double weight, String color){
        
    }
    public double getWeight() {
        return weight;
    }
    public void setWeight(double weight) {
        this.weight = weight;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public double findArea(){
        return 0.0;
    }
}

2. 子类 Circle 圆形

// project = 'Circle', file_name = 'Circle', author = 'AI悦创(黄家宝)'
// time = '2020/3/29 13:40', product_name = Sublime Text3
// # code is far away from bugs with the god animal protecting
//     I love animals. They taste delicious.

public class Circle extends GeometricObject {

    private double radius;
    public Circle(double weight, String color, double radius) {
        super(weight, color);
        this.radius=radius;
    }
    public double getRadius() {
        return radius;
    }
    public void setRadius(double radius) {
        this.radius = radius;
    }
    
    public double findArea(){
        return 3014*radius*radius;
    }

}

3. MyRectangle 代表矩形

// project = 'MyRectangle', file_name = 'MyRectangle', author = 'AI悦创(黄家宝)'
// time = '2020/3/29 13:45', product_name = Sublime Text3
// # code is far away from bugs with the god animal protecting
//     I love animals. They taste delicious.

public class MyRectangle extends GeometricObject {

    private double width;
    private double height;
    public MyRectangle(double weight, String color, double width, double height) {
        super(weight, color);
        this.height=height;
        this.width=width;
    }
    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;
    }
    
    public double findArea(){
        return height*width;
    }

}

4. 测试类 GeometricTest

// project = 'GeometricTest', file_name = 'GeometricTest', author = 'AI悦创(黄家宝)'
// time = '2020/3/29 13:50', product_name = Sublime Text3
// # code is far away from bugs with the god animal protecting
//     I love animals. They taste delicious.

public class GeometricTest {

    public static void main(String[] args) {

        GeometricTest t = new GeometricTest();
        
        Circle c1 = new Circle(5.2, "black", 7.5);
        Circle c2 = new Circle(5.2, "white", 7.5);
        
        MyRectangle m1 = new MyRectangle(2.1,"red",5.7,6.6);
        
        t.displayGeometricObject(c1);
        
        boolean b1 = t.equalsArea(c1, c2);
        boolean b2 = t.equalsArea(c1, m1);
        System.out.println(b1);
        System.out.println(b2);
        
    }
    
    public boolean equalsArea(GeometricObject g1, GeometricObject g2){
        return g1.findArea() == g2.findArea();
    }
    
    public void displayGeometricObject(GeometricObject g){
        System.out.println(g.findArea());
    }
}
3. 几何图形(满分50分) 版本1:满分 10 分 设计抽象 GeometricObject 及其子类 Triangle 和CircleGeometricObject 设计要求如下: ■ 一个名为 color 的Color型的私有数据域,表示对象的颜色 ■ 一个名为 filled 的Boolean型的私有数据域,表示对象是否 ■ 一个名为 dateCreated 的Date 型的私有数据域,表示对象的 ■ 一个无参构造方法。 ■ 一个能创建特定 color 和filled 的有参构造方法。 ■ 相关数据域的访问器和修改器。 ■ 两个个名为 draw 和erase的抽象方法。 ■ 一个名为 getArea的抽象方法。 ■ 一个名为 getPerimeter的抽象方法。 ■ 重写 toString 方法。 Triangle 设计要求如下: ■ 三个名为 side1、side2和 side3 的double 型的私有数据域表 们的默认值是 1.0。要求三个数据域保留 2 位小数。 ■ 一个无参的构造方法创建默认三角形。 ■ 一个能创建带指定 side1、side2和 side3 的有参构造方法。 ■ 所有三个数据域的访问器和修改器方法。 ■ 父类抽象方法的实现。 ■ 重写 toString 方法。 Circle 设计要求如下: ■ 一个名为 radius 的double 型的私有数据域,表示圆的半径,数据域保留2 位小数。 ■ 一个名为 PI 的静态常量,其值为 3.14 ■ 一个无参的构造方法创建默认三角形。 ■ 一个能创建带指定 radius 的有参构造方法。 ■ radius 数据域的访问器和修改器方法。 ■ 父类抽象方法的实现。 ■ 重写 toString 方法。 测试 TestGeometricObject1 设计要求如下: ■ 一个能随机生成 Circle 和Triangle 对象的静态方法 GeometricObject[] RandomCreateGeometricObject() ■ 以随机生成的数组为参数,输出数组中每个对象的基本信息、周长和面积。 ■ 中其它方法的测试 版本2:满分 20 分 将上面的抽象GeometricObject 改为接口,接口只保留其中四个抽象方法,声明 Circle、Triangle 实现该接口,的基本要求如上,同时为每个增加一个将当前对象序列化 到指定文件的方法 writeToFile(File f)。 测试 TestGeometricObject2 设计要求如下: ■ 一个能随机生成 Circle 和Triangle 对象的静态方法,该方法将随机生成的象序列 化到指定的文件 GeometricObjects.dat 中,序列化成功返回真,否则返回假。 Boolean RandomCreateGeometricObject() ■ 将GeometricObjects.dat 文件中对象全部读出,存储到 GeometricObject 对象数组中, 然后以此数组为参数,输出数组中每个对象的基本信息、周长和面积。 ■ 中其它方法的测试。 新增一个Rectangle ,也实现接口 GeometricObject ,同时修改测试 TestGeometricObject2 ,体会开-闭原则。 版本3:满分 20 分 在第2 步的基础上设计实现一个具有 GUI 界面的几何图形绘制系统系统,要求实现根 据选择的几何图形型来绘制和删除相应的图形,其中相关参数应通过界面输入,并可计算 图形的周长和面积。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI悦创|编程1v1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值