JAVA小实验——接口与继承

一、要求📃

(1)定义一个接口Shape,这个接口声明一个抽象方法:求面积 Area,其中返回值为double类型,无返回参数;
(2)定义一个接口Recolorable,这个接口声明一个抽象方法:改变图形颜色Recolorable,其中函数返回无类型(void);
(3)定义一个接口Resizable,这个接口声明一个抽象方法:按比例改变图形大小resizePercent,其中函数返回无类型(void)。
(4)设计一个二维图形类TwoDG和一个三维图形类ThreeDG,使二者继承Recolorable和Shape类;
(5)设计一个圆形类Circle,使其具有TwoDG类特征;
(6)设计一个正方体类Cube,使其具有ThreeDG类特征,同时为其设计一个求体积类volume,其中返回值为double类型,并返回参数。
满足以上6项要求后,需要学生将以下类汇总到一个文件并设计输出样式,运行成功即可满足实验要求。

二、代码💻

package Final;

import java.util.ArrayList;
import java.util.Random;

public class FinalWorkMian {
    public static void main(String[] args) {
        ArrayList<Shape> list=new ArrayList<Shape>();


        list.add(new Circle(ShapeColor.Green,15));
        list.add(new Cube(ShapeColor.Yellow,  18.15));
        list.add(new Circle(ShapeColor.Green,7));
        list.add(new Cube(ShapeColor.White,  8.06));
        list.add(new Circle(ShapeColor.Green,13));
        list.add(new Cube(ShapeColor.White,  12.78));
        list.add(new Circle(ShapeColor.Green,17));
        list.add(new Cube(ShapeColor.Yellow,  3.44));
        displayList(list);
    }



enum ShapeColor {
    Blue, Yellow, Red, Green, White
}

interface Shape {
    double Area() ;
}

interface Resizable {
    void resizePercent(double percentage);
}

interface Recolorable {
    void recolor( ShapeColor sc);
}


abstract static class TwoDG implements Recolorable, Shape {
    protected ShapeColor sc;

    protected int a;

    protected int b;

    protected int c;

    protected int d;



    public TwoDG(){

        sc = ShapeColor.Green;

        a = 0;

        b = 0;

        c = 0;

        d= 0;

    }

    public TwoDG(ShapeColor sc, int a) {

        this.sc = sc;

        this.a = a;

    }

    public TwoDG(ShapeColor sc, int a, int b){

        this.sc = sc ;

        this.a = a;

        this.b = b;

    }

    //public TwoD(ShapeColor sc, int a, int b, int c);

    //public TwoD(ShapeColor sc, int a, int b, int c, int d);



    public TwoDG(TwoDG other){

        this.sc = other.getShapeColor();

        this.a = other.getA();

        this.b = other.getB();

        this.c = other.getC();

        this.d = other.getD();

    }

    public int getA() {

        return this.a;

    }

    public int getB() { return this.b;}

    public int getC() { return this.c;}

    public int getD() { return this.d;}



    public ShapeColor getShapeColor() { return this.sc;}

    public void set(ShapeColor sc, int a , int b , int c , int d ) {

        this.sc = sc;

        this.a = a;

        this.b = b;

        this.c = c;

        this.d = d;

    }

    public void recolor(ShapeColor sc) {

        this.sc = sc ;

    }

    public String toString() {

        String rstr = " (2D ( " + sc + ", ";


        if ( a > 0 ) {

            rstr += a ;

        }

        if ( b > 0 ) {

            rstr += b ;

        }

        if ( c > 0 ) {

            rstr += c ;

        }

        if ( d > 0 ) {

            rstr += d ;

        }

        rstr += " )) ";

        return rstr;

    }

}


abstract static class ThreeDG implements Resizable, Shape {
    protected double a;

    protected ShapeColor sc;

    ThreeDG ThreeDG(){
        return ThreeDG();
    }

    public ThreeDG() {

        this.a = 0;

        this.sc = ShapeColor.White;

    }

    public ThreeDG(ShapeColor sc, double a) {

        this.sc = sc;

        this.a = a;

    }

    public ThreeDG( ThreeDG ThreeDG ) {

        this.sc = ThreeDG.sc;

        this.a = ThreeDG.a;

    }

    public double getA() {

        return this.a;

    }

    public void setShape(ShapeColor sc , double a ) {

        this.sc = sc;

        this.a = a;

    }



    public ShapeColor getShapeColor(){

        return this.sc;

    }

    abstract double volume() ;

    public String toString(){

        String rstr = " (3D ( " + sc + ", " + String.format("%.2f",this.getA()) + " ) ";

        return rstr;

    }

}

static class Circle extends TwoDG {

        public Circle() {

        super();

    }

    public Circle(ShapeColor sc , int rad ) {

        super(sc,rad);

    }

    public void set(ShapeColor sc , int rad ) {

        super.set(sc,rad, 0,0,0);

    }

    @Override
    public double Area() {
        return  3.14 * this.getA() * this.getA() ;
    }

    public int getRadius() {

        return this.getA();

    }



    public String toString() {

        String rstr = " Circle " + super.toString() ;

        return rstr;

    }


    public void recolor(ShapeColor sc) {
        super.recolor(sc);
    }


}

static class Cube extends ThreeDG {

    public Cube() {

        super();

    }

    public Cube(ShapeColor sc, double side) {

        super(sc, side);

    }

    public Cube(Cube Cube) {

        super(Cube.getShapeColor(), Cube.getA());

    }

    @Override
    public double Area() {

        return 6 * this.getA() * this.getA();

    }

    public double volume() {

        return this.getA() * this.getA() * this.getA();

    }

    @Override
    public void resizePercent(double percentage) {
        this.a = (float) (this.getA() * (100 - percentage) / 100);
    }

    public String toString() {

        return " Cube " + super.toString();

    }

}

    public static int getint() {

        int max=25,min=1;
        int rand = (int) (Math.random()*(max-min)+min);
        return rand;

    }

    public static ShapeColor getShapeColor() {

        //{Yellow, Red, Green, White }

        int i = 1 + getint() / 5;

        switch(i){
            case 1: return ShapeColor.White;
            case 2: return ShapeColor.Yellow;
            case 3: return ShapeColor.Green;
            case 4:return ShapeColor.Blue;
            case 5: return ShapeColor.Red;
        }

        return ShapeColor.White;

    }

    public static double getDouble() {

        Random rand = new Random();

        return rand.nextInt(20) + rand.nextDouble();

    }

    static TwoDG getTwoD() {

        int numShapes = 2 ;

        int randInt = getint()/ numShapes ;

        TwoDG randShape = null;

        if ( randInt == 1 ) {

//        int rad = getint();

            randShape = new Circle(ShapeColor.Green, randInt);


        }

        return randShape;

    }


    static ThreeDG getThreeD() {

        int numShapes = 2 ;

        int randInt = getint()/ numShapes ;
        ShapeColor rColor = getShapeColor();

        ThreeDG randShape = null;

        if ( randInt == 1 ) {

//        double side = getDouble();

            randShape = new Cube(rColor, randInt);


        }

        return randShape;

    }



    static void displayList(ArrayList<Shape> list) {

        int i = 1;

        for (Shape s : list) {

            System.out.println("--------------------------------------------------------------------");

            String shape = "";

            String color_changed = "";

            System.out.println("Shape " + i + " : " + s.toString());

            if (s instanceof ThreeDG) {

                ThreeDG s1 = (ThreeDG) s;

                double percent = getDouble();

                System.out.println("Surface Area = " + String.format("%.2f", s1.Area()));

                System.out.println("Volume : " + String.format("%.2f", s1.volume()));

                s1.resizePercent(percent);

                System.out.println("Size reduced by " + percent + "% : " + s1.toString());


                System.out.println("Updated surface Area = " + String.format("%.2f", s1.Area()));

                System.out.println("Updated volume : " + String.format("%.2f", s1.volume()));

                if (s instanceof Cube) {

                    shape = "Cube";

                }

                System.out.println("There is a " + shape);

            }

            if (s instanceof TwoDG) {

                TwoDG s1 = (TwoDG) s;

                ShapeColor change_color = getShapeColor();


                if (change_color != s1.getShapeColor()) {

                    System.out.println("Updated Color : " + change_color);

                    TwoDG sd = (TwoDG) s;

                    sd.recolor(change_color);

                    color_changed = "color changed to " + change_color+". Attention! ";

                }

                System.out.println("Area  : " + String.format("%.2f", s.Area()));

                //System.out.println( s.toString());

                if (s instanceof Circle) {

                    shape = "Circle";

                }

                System.out.println("There is a " + shape + color_changed);

            }

        }
        System.out.println("--------------------------------------------------------------------");
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

烤鲅鱼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值