使用Point center在Java中创建Circle类?

时间:2012-04-12 14:52:38

标签: java

写一个封装圆的概念的类,假设圆具有以下属性:a表示圆的中心的点,圆的半径,整数。包括构造函数,访问器和mutator,以及toString和equals方法。还包括返回圆周长(2 * PI *半径)和面积(PI * radius ^ 2)的方法。

import java.awt.*;


public class Circle {

    private int radius;

    public Circle() {

        radius = 1;
    }

    public Circle(int x, int y, int r) {
        super(x, y, c);
        radius = r;
    }

    public int getRadius() {
        return radius;
    }

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

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

我可以做到这一点,我只是对我的班级添加点构造函数,访问器和mutator有点困惑。

看起来像这样吗?

  protected int x, y;

  public point() {

      setPoint(0,0);

  }

  public point(int coordx, int coordy) {
      setPoint(coordx,coordy);
  }

  public void setPoint(int coordx, int coordy) {
      x = coordx;
      y = coordy;
  }
  public int getX() {
      return x;
  }

  public int getY() {
      return y;
  }

  public String toPrint() {
      return "[" + x + "," + y + "]";
  }

  }

是否可以将两者结合在一起?我试过了,它的每一行都有一个错误,说Circle没有返回类型。任何见解都是可以回报的。再次感谢你们。

4 个答案:

答案 0 :(得分:2)

您正在谈论在单个类中拥有多个对象的构造函数。那是不可能做到的。 Java认为public point()是一种没有返回类型的方法,因此在语法上是不正确的。

你不需要为一个点上课。 Java已经提供java.awt.Point。只需为Point课程添加Circle的课程级别字段,就可以了。

然后

Circle看起来像这样:

public class Circle {

    private int radius;
    private Point point;

    public Circle() {
        point = new Point(0, 0);
        radius = 1;
    }

    public Circle(int x, int y, int r) {
        point = new Point(x, y);
        radius = r;
    }

    public int getRadius() {
        return radius;
    }

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

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

答案 1 :(得分:1)

这里有一些代码,Circle是一个为其添加半径的点的扩展

public class Point {
   public Point(int x, int y) {
      // .. set x and Y coord
   }

   // Getters and Setters
   public int getX() {
      return x;
   }

  public int getY() {
      return y;
  }

  public String toPrint() {
      return "[" + x + "," + y + "]";
  }
  // Your other Point methods...


  private int x = 0;
  private int y = 0;
}


public class Circle extends Point {
  int rad;

  public Circle (int x, int y, int radius) {
    super(x, y);
    rad = radius;
  }
  // Your other Circle methods
}

正如所承诺的那样:没有延期的另一种方式可能是:

class Point {
  int x;
  int y;

  public Point (int x, int y) {
     this.x = x;
     this.y = y;
  }
  // Getter/Setters and other methods
}


public class Circle {

  Point centre = null;
  int radius = 0;

  public Circle(int x, int y, int rad) {
      centre = new Point(x, y);
      radius = rad;
  }
   // Your other Circle methods
}

答案 2 :(得分:1)

我会这样做

import java.awt.Point;

public class Circle {

    private Point center;
    private int radius;

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

    public Circle( int x, int y, int radius ){
        this( new Point( x, y ), radius );
    }

    public Circle( Point center ){
        this( center, 1 );
    }

    public Circle( Point center, int radius ){   
        this.setCenter( center );
        this.radius = radius;
    }

    public int getRadius(){ return this.radius; }
    public Point getCenter(){ return this.center; }

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

    public void setCenter( int x, int y ){
        this.setCenter( new Point( x, y ) );
    } 

    public void setCenter( Point center ){
        this.center = center;
    }

    public boolean equals( Object o ){ 

        if ( o == this ){ return true; }
        if ( o == null || o.getClass() != this.getClass() ){ return false; }

        Circle c = (Circle) o;
        return ( o.radius == this.radius && o.center.equals( this.center ) );
    }

    public string toString(){
        return "Circle[" + this.center.toString() + ", " + this.radius + "]";
    }
}

答案 3 :(得分:0)

你在构造函数中调用超级构造函数:

super(x, y, c);