1

円の面積を計算するプログラムを作成する必要がありますが、プログラムを実行して面積計算がゼロになる値を入力する場合を除いて、すべてが正しいようです。

public class Circle {

    private double radius;
    private double area;

    public Circle() {
        radius = 0;
        area = 0;
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return area;
    }

    public void setArea(double area) {
        area = radius * radius * Math.PI;
    }

    public String toString() {
        return "The radius of the circle is: " + radius + ", and the area is: " + area;
    }
}

テスト コードが toString を呼び出したときに、計算された面積が出力されるようにするには、何を変更する必要がありますか?

4

6 に答える 6

3

現在の半径に基づいて面積を計算するメソッドが必要です。エリアは設定しないでください。

public class Circle {
    private double radius;

    public Circle() {
        radius = 0;
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getArea() {
        return calculateArea();
    }

    private double calculateArea() {
        return radius * radius * Math.PI;
    }

    public String toString() {
        return "The radius of the circle is: " + radius + ", and the area is: "
                + calculateArea();
    }
}

面積を変数に格納したい場合は、半径を設定するときに更新する必要があります。「setArea」から独立して設定しないでください。そうしないと、不整合が発生しやすくなります。また、Josh Bloch の「Effective Java」からのメモ。toString は、計算を複製するのではなく、この「計算された領域」を利用する必要がありますが、パブリック API で toString を呼び出す必要はありません。たとえば、getArea をオーバーライドすると、Circle.toString が期待するものとは異なる動作をするという問題が発生します。そのため、プライベートな「calculateArea」をそこに入れました。

于 2013-11-03T18:46:46.547 に答える
0

これは私のバージョンの Circle_Math クラスで、エントリの検証があり、ジョブを完了するための行/操作とメモリ スペースの最小数があります。ご希望の方はコメントを残してください。

public class circle {
    static double rad;

    public circle() {
        rad = 0;
    }

    public static void setRad() {
        Scanner sc = new Scanner(System.in);

        do {
            while (!sc.hasNextDouble()) {
                sc.next();// this code is to skip the exception created 
            }
            rad = sc.nextDouble();
        } while (rad < 0);

        System.out.println("radius value is:" + rad);

    }

    public static double getCirclearea() {
        return rad * rad * Math.PI;
    }

    public static double getCircumference() {
        return 2 * Math.PI * rad;
    }

}
于 2015-05-19T17:50:06.710 に答える
0
import java.util.Scanner;
public class Circle {
    // variable PI is readable only;
    // constant value
    public static final double PI = 3.14;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        System.out.print("Enter raduis: ");
        double raduis = input.nextDouble();

        double area = PI * raduis * raduis;
        System.out.print("Circle area = " + area);

    }

}
于 2016-02-08T07:26:47.147 に答える
0

メソッドradiusの代わりにarea渡します。setArea

public void setArea( double radius )  
{
    area = (radius)*(radius)*Math.PI;
}   

完全なコード - http://pastebin.com/jggRrUFd

于 2013-11-03T19:30:34.620 に答える
-2

最初にareaを 0 に設定しました。それを変更するメソッドを作成しましたが、それを呼び出しませんでした。だからそれを呼び出します。これを変える:

public String toString() {
    return "The radius of the circle is: " + radius + ", and the area is: " + area;
}

これに:

public String toString() {
    setArea(area); // change the value of the area
    return "The radius of the circle is: " + radius + ", and the area is: " + area;
}
于 2013-11-03T18:55:24.150 に答える