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

急問JAVA問題設計一個類別 Final 使用 Shape

public abstract class Shape {

public double perimeter() { return 0.0; };

public abstract String area() ;

public abstract String getType() ;

} public class ShapeT extends Shape

{

protected double a, b, c;

public ShapeT ()

{ setSides(0.0,0.0,0.0); }

public ShapeT (double a, double b, double c)

{ setSides(a,b,c); }

public void setSides(double a, double b, double c)

{ this.a=a; this.b=b; this.c=c; }

public double perimeter()

{ return a+b+c; }

public String getType()

{ return "三角形"; }

}

public class ShapeC extends Shape {

private double r;

public ShapeC() { r=0; }

public ShapeC(double rr) { r=rr; }

public double perimeter() { return 2.0*Math.PI*r; };

public String getType() { return "圓形"; }

}

請設計一個類別 Final 使用 Shape 的物件方法顯示圖形型態及周長與面積

1 Answer

Rating
  • CK
    Lv 4
    1 decade ago
    Favorite Answer

    1. 你必須在 ShapeT, ShapeC 實作 area

    in ShapeC

    public double area() { return Math.PI*r*r; }

    2. 不會再有其他類別繼承圓, 所以可宣告

    public final class ShapeC extends Shape

    3. 你可以作出直角三角形類別

    public final class ShapeRT extends ShapeT {

    public String getType() { return "直角三角形"; }

    4. area() for ShapeT and ShapeRT

    public double area() {

    double area = 0.0;

    area = a+b+c;

    area *= a+b-c;

    area *= a+c-b;

    area *= b+c-a;

    area = Math.sqrt(area)/4;

    return area;

    }

    三角形面積求法可以到此查看

    http://zh.wikipedia.org/wiki/三角形

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