java class-setX circle方法

时间:2014-03-19 18:34:33

标签: java class methods

我的家庭作业有问题。我创建了一个Circle类,但是在使用setX和setY方法时遇到了一些错误。我不知道如何解决。我的代码如下。谢谢。

import java.awt.Point;

public class Circle {
private Point origin;
private double radius;

public Circle(Point o, double r)
{
    setOrigin(o);
    setRadius(r);
}

public Circle(double xValue, double yValue, double r)
{
    origin.setX(xValue);
    origin.setY(yValue);
    setRadius(r);
}

public Circle()
{
    setX(0.0);
    setY(0.0);
    setRadius(1);
}

public Circle(Circle c)
{
    setOrigin(c.getOrigin());
    setRadius(c.getRadius());
}

Point getOrigin()
{
    return new Point(origin);
}

public void setOrigin(Point p)
{
    origin.setX(p.getX());
    origin.setY(p.getY());
}

public void setX(double value)
{
    origin.setX(value);
}

public double getX()
{
    return origin.getX();
}

public void setY(double value)
{
    origin.setY(value);
}

public double getY()
{
    return origin.getY();
}

public void setRadius(double value)
{
    radius = value;
}

public double getRadius()
{
    return radius;
}

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

public String toString()
{
    return "(" + origin.getX() + ", " + origin.getY() + ", " + radius             + ")";
}

public boolean equals(Circle c)
{
    if (origin.getX() == c.getX() && origin.getY() == c.getY() &&
    getRadius() == c.getRadius())
    {
        return true;
    }
    else
    {
        return false;
    }
}
public boolean doesOverlap(Circle oC)
{
    double distance = Math.sqrt((Math.pow(getX() - oC.getX(), 2) +    Math.pow(getY()-oC.getY(), 2)));

    if ((radius + oC.radius) > distance)
    {
        return true;    
    }
    else
    {
        return false;
    }

}

}

public class Point {
private double x;
private double y;

public Point(double xValue, double yValue)
{
    x = xValue;
    y = yValue;
}

public Point(Point p) {
    this(p.x, p.y);
}

public Point() {
    this(0, 0);
}

public void setX(double xValue)
{
    x = xValue;
}

public double getX()
{
    return x;
}

public void setY(double xValue)
{
    y = xValue;
}

public double getY()
{
    return y;
}

public boolean equals(Point otherPoint)
{
    return (this.x == otherPoint.x) && (this.y == otherPoint.y);
}

public String toString() {
    return "(" + x + ", " + y + ")";
}
}

1 个答案:

答案 0 :(得分:0)

查看Point的API。没有像setX(...)

这样的方法

此外,我认为你应该初始化它,特别是在你的第二个和第三个构造函数

public Circle(double xValue, double yValue, double r){...}
public Circle(){...}

经由

origin = new Point();