使用点类计算圆的面积和周长

时间:2014-11-23 21:15:26

标签: java class constructor point

我最初编写了以下Point类:

public class Point {

    private double x;
    private double y;

    public Point (double x, double y) {

        this.x = x;
        this.y = y;
    }
}

我尝试使用点类作为存储圆的原点的方法,以便稍后我可以计算出2个圆相交:

public class Circle {

    double radius;
    Point origin;

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

    public double circumfurence (double radius) {
        return 2 * radius * Math.PI;
    }

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

    public static void main(String[] args) {
        Circle c = new Circle((3.0,3.0), 3.0);
        System.out.println(c.area());
        System.out.println(c.circumfurence());
    }

}

但是,当我尝试编译这两个文件时,我收到以下错误:

Circle.java:20: error: ')' expected
    Circle c = new Circle((3.0,3.0), 3.0);
                              ^

如果我删除(3.0,3.0)周围的括号,我会收到此错误:

Circle.java:20: error: constructor Circle in class Circle cannot be applied to given types;
    Circle c = new Circle(3.0,3.0, 3.0);
               ^
required: Point,double
found: double,double,double
reason: actual and formal argument lists differ in length

编辑:谢谢,但我仍然收到错误:

Circle.java:21: error: method area in class Circle cannot be applied to given types;
    System.out.println(c.area());
                        ^
required: double
found: no arguments
reason: actual and formal argument lists differ in length

1 个答案:

答案 0 :(得分:3)

CirclePoint实例作为其第一个构造函数参数

Circle c = new Circle(new Point(3.0,3.0), 3.0);