http://www.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html
This is my question ,I m doing 2.1 Exercise: The Circle and Cylinder Classes
below here is my coding
---------------------------------------------------
Circle class
--------------------------------------------------
public class Circle
{
private double radius;
private String color ="red";
Circle()
{
radius=1.0;
}
Circle(double radius)
{
this.radius=radius;
}
public double getRadius()
{
return radius;
}
public double getArea()
{
return radius*radius*Math.PI;
}
public String toString()
{
return "radius" +radius+ "Area" +getArea();
}
}
-----------------------------------------------------
Cylinder class
--------------------------------------------------------
public class Cylinder extends Circle { //save as "Cylinder.java"
private double height; // private variable
// Constructor with default color, radius and height
public Cylinder() {
super(); // call superclass no-arg constructor Circle()
height = 1.0;
}
// Constructor with default radius, color but given height
public Cylinder(double height) {
super(); // call superclass no-arg constructor Circle()
this.height = height;
}
// Constructor with default color, but given radius, height
public Cylinder(double radius, double height) {
super(radius); // call superclass constructor Circle(r)
this.height = height;
}
// A public method for retrieving the height
public double getHeight() {
return height;
}
// A public method for computing the volume of cylinder
// use superclass method getArea() to get the base area
public double getVolume() {
return getArea()*height;
}
public String toString() { // in Cylinder class
return "Cylinder: subclass of " + super.toString() // use Circle's toString()
+ " height=" + height;
}
}
------------------------------------------
Test program
------------------------------------------
public class TestCylinder { // save as "TestCylinder.java"
public static void main (String[] args) {
// Declare and allocate a new instance of cylinder
// with default color, radius, and height
Cylinder c1 = new Cylinder();
System.out.println("Cylinder:"
+ " radius=" + c1.getRadius()
+ " height=" + c1.getHeight()
+ " base area=" + c1.getArea()
+ " volume=" + c1.getVolume());
// Declare and allocate a new instance of cylinder
// specifying height, with default color and radius
Cylinder c2 = new Cylinder(10.0);
System.out.println("Cylinder:"
+ " radius=" + c2.getRadius()
+ " height=" + c2.getHeight()
+ " base area=" + c2.getArea()
+ " volume=" + c2.getVolume());
// Declare and allocate a new instance of cylinder
// specifying radius and height, with default color
Cylinder c3 = new Cylinder(2.0, 10.0);
System.out.println("Cylinder:"
+ " radius=" + c3.getRadius()
+ " height=" + c3.getHeight()
+ " base area=" + c3.getArea()
+ " volume=" + c3.getVolume());
}
System.out.println(c3);
}
my problem is the tostring method result can't show up but the other result will show up? anyone here know about override tostring method?
and explain to me what is tostring method use for?
thks a lot