円が次の属性を持つと仮定して、円の概念をカプセル化するクラスを作成します: 円の中心を表す 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 というエラーがありました。どんな洞察も返礼品です。みんなありがとう。