Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

Java area()抽象方法求解?....簡單一點...

)建立 Areaface介面,包含area()抽象方法。

(2)建立父類別 TwoPoint ,包含點座標 x 與 y 陣列,並提供 set 與 get 方法。

class TwoPoint

{

protected double[] x = new double[2];

protected double[] y = new double[2];

public void setPoint(double a, double b, int i)

{

// 設定 x[i] 等於 a,y[i] 等於 b。

}

public double getX(int i)

{

//取得x[i]的座標

}

(3)建立 TwoPoint 類別的子類別 Line,包含 getBase() 與 getHeight() 方法。

public double getBase()

{

return Math.abs(getX(0)-getX(1));

}

public double getBase()

{

return Math.abs(getY(0)-getY(1));

}

(4)建立 Line類別的子類別 Rectangle ,實作 area() 計算長方形的面積。

(5)建立 Line類別的子類別 Triangle ,實作 area() 計算三角形的面積。

(6)在 Ex 類別中,寫一個main()方法,分別建立 Rectangle 與 Triangle 類別物件,與分別顯示長方形與三角形的座標與面積。

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    請參考我的做法

    interface Areaface {

    public double area();

    }

    class TwoPoint {

    protected double[] x = new double[2];

    protected double[] y = new double[2];

    public void setPoint(double a, double b, int i) {

    x[i] = a;

    y[i] = b;

    }

    public double getX(int i) {

    return x[i];

    }

    public void setX(double a, int i) {

    x[i] = a;

    }

    public double getY(int i) {

    return y[i];

    }

    public void setY(double a, int i) {

    y[i] = a;

    }

    }

    class Line extends TwoPoint {

    public double getBase() {

    return Math.abs(getX(0)-getX(1));

    }

    public double getHeight() {

    return Math.abs(getY(0)-getY(1));

    }

    }

    class Rectangle extends Line implements Areaface {

    public double area() {

    return getBase() * getHeight();

    }

    }

    class Triangle extends Line implements Areaface {

    public double area() {

    return getBase() * getHeight() / 2;

    }

    }

    public class Ex {

    public static void main(String[] args) {

    Rectangle rec = new Rectangle();

    rec.setPoint(1, 4, 0);

    rec.setPoint(5, 8, 1);

    System.out.println("Area of the rectangle: " + rec.area());

    Triangle tri = new Triangle();

    tri.setPoint(3, 7, 0);

    tri.setPoint(7, -2, 1);

    System.out.println("Area of the triangle: " + tri.area());

    }

    }

    • Commenter avatarLogin to reply the answers
  • 1 decade ago

    這個東西好像太專業了吧,應該沒有人懂吧...你可以解釋一下這是什麼東西嗎?

    Source(s):
    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.