D
David McCallum
I have defined the following classes:
When the main method is called, Circle.toString is called recursively. The
intention is to use Shape.toString to create the X and Y parts of the
string.
Can anyone tell me why?
public class Shape {
double x;
double y;
Shape(){
x=0;
y=0;
}
Shape(double newX, double newY){
x=newX;
y=newY;
}
public String toString() {
return "X="+new Double(x).toString()+"\n"+
"Y="+new Double(y).toString()+"\n";
}
}
public class Circle extends Shape {
double radius;
Circle(){
super();
}
Circle(double newX, double newY, double newRadius) {
super(newX, newY);
radius=newRadius;
}
public String toString() {
return ((Shape)this).toString()+
"R="+new Double(radius).toString()+"\n";
}
public static void main(String[] args){
Circle c1=new Circle();
Circle c2=new Circle(1, 2, 3);
System.out.println(c1);
System.out.println(c2);
}
}
When the main method is called, Circle.toString is called recursively. The
intention is to use Shape.toString to create the X and Y parts of the
string.
Can anyone tell me why?
public class Shape {
double x;
double y;
Shape(){
x=0;
y=0;
}
Shape(double newX, double newY){
x=newX;
y=newY;
}
public String toString() {
return "X="+new Double(x).toString()+"\n"+
"Y="+new Double(y).toString()+"\n";
}
}
public class Circle extends Shape {
double radius;
Circle(){
super();
}
Circle(double newX, double newY, double newRadius) {
super(newX, newY);
radius=newRadius;
}
public String toString() {
return ((Shape)this).toString()+
"R="+new Double(radius).toString()+"\n";
}
public static void main(String[] args){
Circle c1=new Circle();
Circle c2=new Circle(1, 2, 3);
System.out.println(c1);
System.out.println(c2);
}
}