Write code for multiple inheritance using java

 寫出多重繼承的程式碼用java

請以 Java 或 C++撰寫實做一簡單幾何圖形類別系統(class hierarchy),可以表示圓

形、方形與矩形。圓形、方形與矩形之類別名稱分別為 Circle、Square 與 Rectangle。

三者的共同祖先類別 Shape 是一個抽象類別,代表所有可能的幾何圖形。Shape 下的

兩個具體子類別分別為 Circle 及 Square,Square 下則有 Rectangle 類別。

我們希望每一幾何物件均能提供以下方法:

透過 getX(),getY()可得到幾何中心點的 X 與 Y 座標。

透過 getArea()可得到圖形面積。

透過 getPerimeter()可得到圖形周長。

透過 translate(double x, double y):void 可對圖形進行位移。

透過 scale(double f):void 可對圖形進行放大或縮小。

透過 toString():String 可得到該圖形的形狀種類、中心與長寬或半徑的文字

資訊。

其他類別須額外提供以下方法:

Circle 物件須提供 getRadius():double,可以取得物件半徑。

Square 物件須提供 getSide():double,可以取得邊長。

Rectangle 物件須提供 getHeight(),getWidth():double,可以取得矩形的高

與寬。

每一類別僅需提供一建構式(constructor),其參數格式要求如下:

a. Shape(x,y),b. Rectangle(x,y,w,h),c. Square(x,y,s),d.Circle(x,y,r)。

所有參數的型態(type)均為 double,其中 x 與 y 為幾何圖形中心的 X 與 Y 座

標,r 為圓形半徑,w 與 h 為矩形的寬與高而 s 則是方形的邊長。請根據以上要

求,以 Java 實做此四類別(class)。除須符合上述要求之外,實做時尚須遵循一般

物件導向與軟體設計原則,尚須避免程式碼重複、產生無用欄位,以及資訊隱藏不

全等缺點。(20 分

答案

(一)類別圖



(二)

abstract class Shape {
double x;
double y;
abstract string toString ();
abstract void scale (double f);
abstract double getArea();
abstract double getPerimeter ();
double getX(){
return x;
}
double getY(){
return y;
}
void translate(double deltax , double deltay){
x+=deltax;
y+=deltay;
}
Shape (double x, double y){
this.x=x;
this.y=y;
}
}
class Circle extends Shape {
double r;
Circle (double x, double y, double r){
super (x,y);
this.r=r;
}
double getArea(){
return r*r*Math.PI;
}
double getPerimeter (){
return 2*r*Math.PI;
}
void scale(double f){
r*=f;
}
string toString (){
return "shape is circle"+",x is "+x+",y is "+y+
",r is "+r;
}
double getRadius(){
return r;
}
}
class Square extends Shape {
double s;
Square (double x, double y, double s){
super (x,y);
this.s=s;
}
double getArea(){
return s*s;
}
double getPerimeter (){
return 2*s+2*s;
}
void scale(double f){
s*=f;
}
string toString (){
return "shape is square"+",x is "+x+",y is "+y+
",Side is "+s;
}
double getSide(){
return s;
}
}
class Rectangle extends Shape {
double w;
double h;
Rectangle (double x, double y, double w, double h){
super (x,y,0);
this.w=w;
this.h=h
}
double getArea(){
return w*h;
}
double getPerimeter (){
return 2*w+2*h;
}
void scale(double f){
w*=f;
h*=f
}
string toString (){
return "shape is rectangle"+",x is "+x+",y is "+y+
",Width is "+w+", height is "+h;
}
double getHeight(){
return h;
}
double getWidth(){
return w;
}
}

Chatgpt 回答

以下是 Java 中的幾何圖形類別系統的實作,包括 `Shape` 抽象類別以及其子類別 `Circle`、`Square` 和 `Rectangle`:


```java

abstract class Shape {

    protected double x;

    protected double y;


    public Shape(double x, double y) {

        this.x = x;

        this.y = y;

    }


    public double getX() {

        return x;

    }


    public double getY() {

        return y;

    }


    public abstract double getArea();

    public abstract double getPerimeter();

    public abstract void translate(double deltaX, double deltaY);

    public abstract void scale(double factor);

    public abstract String toString();

}


class Circle extends Shape {

    private double radius;


    public Circle(double x, double y, double radius) {

        super(x, y);

        this.radius = radius;

    }


    public double getRadius() {

        return radius;

    }


    public double getArea() {

        return Math.PI * radius * radius;

    }


    public double getPerimeter() {

        return 2 * Math.PI * radius;

    }


    public void translate(double deltaX, double deltaY) {

        x += deltaX;

        y += deltaY;

    }


    public void scale(double factor) {

        radius *= factor;

    }


    public String toString() {

        return "Circle [x=" + x + ", y=" + y + ", radius=" + radius + "]";

    }

}


class Square extends Shape {

    private double side;


    public Square(double x, double y, double side) {

        super(x, y);

        this.side = side;

    }


    public double getSide() {

        return side;

    }


    public double getArea() {

        return side * side;

    }


    public double getPerimeter() {

        return 4 * side;

    }


    public void translate(double deltaX, double deltaY) {

        x += deltaX;

        y += deltaY;

    }


    public void scale(double factor) {

        side *= factor;

    }


    public String toString() {

        return "Square [x=" + x + ", y=" + y + ", side=" + side + "]";

    }

}


class Rectangle extends Square {

    private double height;


    public Rectangle(double x, double y, double width, double height) {

        super(x, y, width);

        this.height = height;

    }


    public double getHeight() {

        return height;

    }


    public double getWidth() {

        return side;

    }


    public double getArea() {

        return side * height;

    }


    public String toString() {

        return "Rectangle [x=" + x + ", y=" + y + ", width=" + side + ", height=" + height + "]";

    }

}

```


上述代碼定义了 `Shape` 抽象類別以及其子類別 `Circle`、`Square` 和 `Rectangle`。每个类都按照要求提供了相应的方法和构造函数,以便创建和操作不同类型的几何图形对象。


Comments

Popular posts from this blog

Format date as yyyy-mm-dd using vbscript

How to write data into a excel file using vbscript