用Java计算圆的面积

时间:2013-11-03 18:40:46

标签: java constructor geometry

我需要编写一个程序来计算圆的面积,我似乎把一切都搞定了,除非我运行程序并输入值,区域计算结果为零。

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时输出计算区域?

6 个答案:

答案 0 :(得分: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中调用任何东西。例如,如果你覆盖了getArea,这将是一个问题,这意味着它的行为将与你的Circle.toString预期的不同。这就是我将私人“calculateArea”放在那里的原因。

答案 1 :(得分:0)

radius方法中传递area而不是setArea

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

完整代码 - http://pastebin.com/jggRrUFd

答案 2 :(得分: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;
    }

}

答案 3 :(得分: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);

    }

}

答案 4 :(得分:0)

public static double getAreaOfCircle(int radius){
        return Math.PI*radius*radius;
    }

答案 5 :(得分:-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;
}