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

java程式

請java高手可以告訴我~這個程式如何寫以及說明,我實在想不出

來要如何寫,也完全找不到相關資料

有一interface名為Shape,內有兩個成員方法:一為計算面積computeArea(),另為計算周長computeCircumference()。(方法的引數與傳回值自訂)

寫兩個類別分別為Circle和Rectangle,用以實作上述介面,並驗証結果。

2 Answers

Rating
  • 1 decade ago
    Best Answer

    參考看看

    interface Shape {

    public double computeArea();

    public double computeCircumference();

    }

    class Circle implements Shape {

    double radius = 0.0;

    double circlearea = 0.0;

    double circumference = 0.0;

    public Circle( double radius ) {

    this.radius = radius;

    }

    public double computeArea() {

    circlearea = radius * radius * 3.14;

    return circlearea;

    }

    public double computeCircumference() {

    circumference = 2 * 3.14 * radius;

    return circumference;

    }

    }

    class Rectangle implements Shape {

    private double wide = 0.0;

    private double length = 0.0;

    private double area = 0.0;

    private double circumference = 0.0;

    public Rectangle( double wide , double length ) {

    this.wide = wide;

    this.length = length;

    }

    public double computeArea() {

    area = wide * length;

    return area;

    }

    public double computeCircumference() {

    circumference = ( 2 * wide ) + ( 2 * length );

    return circumference;

    }

    }

    public class main1 {

    public static void main(String[] args) {

    double rectanglearea = 0.0;

    double recCircumference = 0.0;

    double circlearea = 0.0;

    double cirCircumference = 0.0;

    Circle circle1 = new Circle(12);

    Rectangle rectangle1 = new Rectangle(10,10);

    circlearea = circle1.computeArea();

    rectanglearea = rectangle1.computeArea();

    cirCircumference = circle1.computeCircumference();

    recCircumference = rectangle1.computeCircumference();

    System.out.println("Rectangle 's circumference is :" + recCircumference);

    System.out.println("Rectangle 's area is :" + rectanglearea);

    System.out.println("Circle 's circumference is :" + cirCircumference);

    System.out.println("Circle 's area is :" + circlearea);

    }

    }

    2009-07-16 23:14:25 補充:

    報告完畢

    2009-07-16 23:18:21 補充:

    我是初學者,只會簡單的方法,

    應該還有更好的方法吧

    2009-07-16 23:22:08 補充:

    執行結果

    compile-single:

    run-single:

    Rectangle 's circumference is :40.0

    Rectangle 's area is :100.0

    Circle 's circumference is :75.36

    Circle 's area is :452.16

    建置成功(總時間:0 秒)

    2009-07-16 23:22:20 補充:

    報告完畢

    2009-07-17 08:30:42 補充:

    Thanks , ck boss.

    2009-07-17 08:45:27 補充:

    about private , i think i forgot ,

    but also thank you ck boss ........really.

    2009-07-17 09:06:42 補充:

    就如ck大大說的,將3.14 改為Math.PI,

    我後來想想,的確精確度會提高 ,because pi not only 3.14.

    哈哈,這時就看到我菜的地方啦(慘~被發現~三十六計~走為上策~"酸")

    2009-07-17 11:13:08 補充:

    經過ck 大大的指教,改了一下,如有錯,還請ck大大見諒與指教,謝謝

    程式碼如下 :

    interface Shape {

    public double computeArea();

    public double computeCircumference();

    }

    class Circle implements Shape {

    private double radius = 0.0;

    private double circleArea = 0.0;

    private double circumference = 0.0;

    2009-07-17 11:13:39 補充:

    public Circle( double radius ) {

    this.radius = radius;

    circleArea = radius * radius * Math.PI;

    circumference = 2 * Math.PI * radius;

    }

    public double getCircleArea() {

    return circleArea;

    }

    2009-07-17 11:13:50 補充:

    public double getCircleCircumference() {

    return circumference;

    }

    public double computeArea() {

    circleArea = getCircleArea();

    return circleArea;

    }

    2009-07-17 11:14:01 補充:

    public double computeCircumference() {

    circumference = getCircleCircumference();

    return circumference;

    }

    }

    2009-07-17 11:14:11 補充:

    class Rectangle implements Shape {

    private double wide = 0.0;

    private double length = 0.0;

    private double rectangleArea = 0.0;

    private double circumference = 0.0;

    2009-07-17 11:14:21 補充:

    public Rectangle( double wide , double length ) {

    this.wide = wide;

    this.length = length;

    rectangleArea = wide * length;

    circumference = 2 * ( length + wide );

    }

    2009-07-17 11:14:31 補充:

    public double getRectangleArea() {

    return rectangleArea;

    }

    public double getRectangleCircumference() {

    return circumference;

    }

    2009-07-17 11:14:42 補充:

    public double computeArea() {

    rectangleArea = getRectangleArea();

    return rectangleArea;

    }

    public double computeCircumference() {

    circumference = getRectangleCircumference();

    return circumference;

    }

    }

    2009-07-17 11:14:50 補充:

    public class main1 {

    public static void main(String[] args) {

    double rectanglearea = 0.0;

    double recCircumference = 0.0;

    double circlearea = 0.0;

    double cirCircumference = 0.0;

    2009-07-17 11:15:16 補充:

    Circle circle1 = new Circle(12);

    Rectangle rectangle1 = new Rectangle(10,10);

    circlearea = circle1.computeArea();

    rectanglearea = rectangle1.computeArea();

    cirCircumference = circle1.computeCircumference();

    2009-07-17 11:15:40 補充:

    recCircumference = rectangle1.computeCircumference();

    System.out.println("Rectangle 's circumference is : " + recCircumference);

    System.out.println("Rectangle 's area is : " + rectanglearea);

    2009-07-17 11:15:48 補充:

    System.out.println("Circle 's circumference is : " + cirCircumference);

    System.out.println("Circle 's area is : " + circlearea);

    }

    }

    2009-07-17 11:19:26 補充:

    執行結果:

    Rectangle 's circumference is : 40.0

    Rectangle 's area is : 100.0

    Circle 's circumference is : 75.39822368615503

    Circle 's area is : 452.3893421169302

    建置成功(總時間:0 秒)

    2009-07-17 11:19:35 補充:

    報告完畢

    2009-07-17 11:21:58 補充:

    如怕剪貼失敗,

    請到我的blog 看完整程式碼,謝謝

    http://walteranddaniel1.pixnet.net/blog/post/26583...

    2009-07-17 11:22:43 補充:

    再度給他報告完畢的啦

    2009-07-17 11:25:33 補充:

    說一下,高手是ck,

    我只是個吃菜的鳥,

    菜鳥啦

  • CK
    Lv 4
    1 decade ago

    public Circle( double radius ) {

    this.radius = radius;

    circlearea = radius * radius * Math.PI;

    circumference = 2 * Math.PI * radius;

    }

    better set private radius, circlearea, circumference;

    2009-07-17 00:55:41 補充:

    and need getter and setter functions.

    public double getRadius() {return radius;}

    public double getCircleArea()......

Still have questions? Get your answers by asking now.