創建具有兩個私有成員的類Point:int x和int y,表示點的座標。類點有兩個構造函數:使用類點類圓的程序java
- 點()指派零以變量x和y,和值x和y分配給變量this.x和這個
- 點(INT的x,int y)對。 y,分別。
此外,編寫一個成員方法public void move(int x,int y)將值x和y分配給私有成員並覆蓋方法public String toString()返回Point(x + 「+ y)。
創建類有兩個私有成員的圓:int radius和Point center。寫入以下counstructors:
- 圈() - 假定默認半徑爲1且中心(0,0)
- 圈(INT半徑) - 指派整數半徑this.radius(在0中心, 0)
- Circle(點中心) - 將點中心分配給this.center(半徑1)
- Circle(int radius,Point center) - 將整數半徑指定給this.radius,並將中心指定給this.center
另外,寫下面的我MBER方法:
- 雙區() - 其計算圓的面積
- 雙圓周() - 其計算的圓的周長
- 字符串的ToString() - 它返回圓的半徑和中心的字符串表示。
編寫一個驅動類Lab2.java,它實例化幾個Point和Circle類型的對象(至少4個,使用4個不同的構造函數)並測試所有的成員方法。
class Point {
private int x;
private int y;
public Point(){
x = y = 0;
}
public Point(int x, int y){
this.x = x;
this.y = y;
}
public void move (int x, int y){
this.x = x;
this.y = y;
}
public String toString(){
String s = "x: " + x + "y: " + y;
return s;
}
}
class Circle{
private int radius;
private Point center;
public Circle(){
int radius = 1;
Point center = new Point(0,0);
}
public Circle(int radius){
int radius = this.radius(center(0,0));
}
public Circle(Point center){
Point center = this.center(radius (1));
}
public Circle(int radius, Point center){
int radius = this.radius;
Point center = this.center;
}
double area() {
return Math.PI*radius*radius;
}
double circumference(){
return 2*Math.PI*radius;
}
String toString(){
String s = "radius: " + radius + "center: " + center;
return s;
}
}
class Lab21{
public static void main(String[] args){
Circle c = new Circle();
System.out.println(c);
c.move(4,2);
System.out.println(c);
c.resize(5,2);
System.out.println(c);
point center = new Point(3,3);
Circle s = new Circle (6, 1, center);
System.out.println(s);
}
}
這是目前我得到的錯誤:
Lab21.java:37: error: variable radius is already defined in constructor Circle(int)
int radius = this.radius(center(0,0));
^
Lab21.java:37: error: cannot find symbol
int radius = this.radius(center(0,0));
^
symbol: method center(int,int)
location: class Circle
Lab21.java:41: error: variable center is already defined in constructor Circle(Point)
Point center = this.center(radius (1));
^
Lab21.java:41: error: cannot find symbol
Point center = this.center(radius (1));
^
symbol: method radius(int)
location: class Circle
Lab21.java:45: error: variable radius is already defined in constructor Circle(int,Point)
int radius = this.radius;
^
Lab21.java:46: error: variable center is already defined in constructor Circle(int,Point)
Point center = this.center;
^
Lab21.java:57: error: toString() in Circle cannot override toString() in Object
String toString(){
^
attempting to assign weaker access privileges; was public
Lab21.java:67: error: cannot find symbol
r.move(4,2);
^
symbol: variable r
location: class Lab21
Lab21.java:69: error: cannot find symbol
r.resize(5,2);
^
symbol: variable r
location: class Lab21
Lab21.java:72: error: cannot find symbol
point center = new Point(3,3);
^
symbol: class point
location: class Lab21
- 在所有應有的尊重下,您不應該先向我們展示您的代碼嘗試嗎?那麼如果這不起作用,您是不是應該問我們一個具體而且可以回答的問題,而不是簡單地在您的網站上傾銷您的任務要求?請通過[遊覽],[幫助]和[如何提出一個很好的問題](http://stackoverflow.com/help/how-to-ask)部分來查看本網站的工作原理並幫助您改善您當前和未來的問題,這可以幫助您獲得更好的答案。 –
做你的功課! – mmking
我正在這樣做,正如你可以在我的編輯中看到的.......但我得到這麼多的錯誤,我沒有看到實際發佈的任何一點 – I31