Hello,
I am having trouble with the code posted below. The code has some pre-compile errors, which I will list here:
//cannot find symbol
//symbol: method showStyle(hsdynoveriddenshapes1.TwoDShape)
//location: class hsdynoveriddenshapes1.HSDynOveriddenShapes1
The error has to do with the showStyle() method called in lines 106 and 107. If I write similar print statements but for the showDim() method I get similar errors.
I want the code to at least output this:
object is triangle
style is right
Area is 48.0
object is triangle
style is isosceles
Area is 24.5
object is null
style is null
Area is 0.0
If I could use the getDim() method to work that would be good too. Here is the code.
[highlight=java]
package hsdynoveriddenshapes1;
class TwoDShape {
private double width;
private double height;
private String name;
// A default constructor.
TwoDShape() {
width = height = 0.0;
name = "null";
//System.out.println("width is: " + width); // This prints with correct object.
}
// Parameterized constructor.
TwoDShape(double w, double h, String n) {
width = w;
height = h;
name = n;
//System.out.println("name is: " + name); This prints with correct object.
}
// Construct object with equal width and height.
TwoDShape(double x, String n) {
width = height = x;
name = n;
}
// Construct an object from an object.
TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
name = ob.name;
System.out.println("name is: " + name); //Why won't this print?
}
// Accessor methods for width and height.
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
String getName() { return name; }
void showDim() {
System.out.println("Width and height are " +
width + " and " + height);
}
double area() {
System.out.println("area() must be overridden");
return 0.0;
}
}
// A subclass of TwoDShape for Triangles.
class Triangle extends TwoDShape {
private String style;
// A default constructor.
Triangle() {
super();
style = "null";
}
// Constructor for Triangle.
Triangle(String s, double w, double h) {
super(w, h, "triangle");
style = s;
}
// Construct an isosceles triangle.
Triangle(double x) {
super(x, "triangle"); // call superclass constructor
style = "isosceles";
}
// Construct an object from an object.
Triangle(Triangle ob) {
super(ob); // pass object to TwoDShape constructor
style = ob.style;
System.out.println("style is " + style);
}
@Override
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
System.out.println("Triangle is " + style);
}
}
public class HSDynOveriddenShapes1 {
public static void main(String[] args) {
TwoDShape shapes[] = new TwoDShape[3];
shapes[0] = new Triangle("right", 8.0, 12.0);
shapes[1] = new Triangle(7.0);
shapes[2] = new Triangle();
//shapes[3] = new TwoDShape(10,20,"Generic");
for(int i=0; i < shapes.length; i++) {
System.out.println("object is " + shapes[i].getName());
System.out.println("Area is " + shapes[i].area());
System.out.println("Style is " + showStyle(shapes[i]));
System.out.println("Style is " + shapes[i].showStyle());
System.out.println();
}
}
}
[/java]