Movatterモバイル変換


[0]ホーム

URL:


Programming & Design
Question:
problem with override tostring method in java?
All
2013-07-28 06:06:46 UTC
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
Three answers:
alex
2013-07-28 06:46:20 UTC
Not sure what the problem is when I run:

System.out.println(c3);

I get the following output

"Cylinder: subclass of radius: 2.0 Area: 12.566370614359172 height=10.0"

Is that what you are looking for??



Edit: Where "radius: 2.0 Area: 12.566370614359172" is from circle.toString()

and the other text "Cylinder: subclass of " and " height=10.0" is from cylinder.toString()



If you show me what the actual output is and what you expect to get I can help you out
villagran
2016-08-04 10:17:24 UTC
A category is a blueprint for an object. An object can be a car, an Edsel, a steer clear of Viper. A class is made from ways and attributes. A method is a behavior of the category. In our keep away from Viper class, that you may say "flip left" or "pace up." In code, if we've a DodgeViper object and a turnLeft process, then to name the procedure the code would seem anything like DodgeViper.TurnLeft(< AnyParameters > ); An attribute is a confidential variable of the class. For example, that you can say a ward off Viper's color is equal to blue. I am hoping this helps.
Puter_Notgeek
2013-07-28 06:30:25 UTC
You can't do it that way.



Look here, it will show you why and how.



http://stackoverflow.com/questions/6386343/how-to-call-a-super-method-ie-tostring-from-outside-a-derived-class


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
about -legalese
Loading...

[8]ページ先頭

©2009-2025 Movatter.jp