3

円が次の属性を持つと仮定して、円の概念をカプセル化するクラスを作成します: 円の中心を表す Point と、円の半径 (整数)。コンストラクター、アクセサーとミューテーター、メソッド toString と equals を含めます。円の周囲 (2*PI*radius) と面積 (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;
    }
}

ここまではできましたが、ポイント コンストラクター、アクセサー、およびミューテーターをクラスに追加することに少し混乱しています。

それはこのように見えるでしょうか?

  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 + "]";
  }

  }

1つのクラスで両方を組み合わせることは可能ですか? 試してみたところ、その中のすべての行に、Circle has no return type というエラーがありました。どんな洞察も返礼品です。みんなありがとう。

4

4 に答える 4

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;
    }
}
于 2012-04-12T14:59:11.317 に答える
2

ここでいくつかのコード、円はそれに半径を追加する点の延長です

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
}
于 2012-04-12T15:01:35.167 に答える
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 + "]";
    }
}
于 2012-04-12T15:09:19.470 に答える
0

コンストラクター内でスーパーコンストラクターを呼び出します。

super(x, y, c); 
于 2012-04-12T14:58:24.047 に答える