printing of area

i have this driver file

Rectangle rect =new Rectangle(12,7);

System.out.println(rect.toString());

here is the super class

public class TwoDShape

{

privatedouble width;

privatedouble height;

/*

The defaultConstructor for objects of class TwoDShape

*/

public TwoDShape()

{

}

/*

* A Constructor for objects of class TwoDShape

* param width the width of the shape

* param height the height of the shape

*/

public TwoDShape(double width,double height)

{

this.width = width;

this.height = height;

}

/*

* getter method for width

* return the width

*/

publicdouble getWidth()

{

return width;

}

/*

* getter method for height

* return the height

*/

publicdouble getHeight()

{

return height;

}

/*

* setter method for width

* param width the new value for width

*/

publicvoid setWidth(double width)

{

this.width = width;

}

/*

* setter method for height

* param height the new value of height

*/

publicvoid setHeight()

{

this.height = height;

}

/*

* produces a string representation of the class

* return the String represntation of the class attributes

*/

public String toString ()

{

String temp =new String("\nWidth is: " + width +"\nHeight is: " + height);

return temp;

}

}

my rectangle class is as below

publicclass Rectangleextends TwoDShape

{

String styleAttribute ="Rectangle";

double area;

public Rectangle(double width,double height)//constructor with two parameters

{

super(width, height);

}

//Calculates the area of the rectangle

publicdouble calculateArea()

{

area = getWidth() * getHeight();

return area;

}

public String getStyleAttribute()

{

return styleAttribute;

}

//Returns a string representing the values of all the attributes of the rectangle

public String toString()

{

// call toString() method of superclass

String temp =new String ("Style Attribute is: " + styleAttribute +"\nRectangle area is: "+ area + super.toString());

return temp;

}

}

why does my area print out at 0 and how can i solve this problem, thanks!!!!

[4960 byte] By [alex_waztica] at [2007-11-26 20:15:10]
# 1

String temp = new String ("Style Attribute is: " + styleAttribute + "\nRectangle area is: "+ area + super.toString());

You are printing the value of area, but until you've called this method:

public double calculateArea()

{

area = getWidth() * getHeight();

return area;

}

the area is 0.

Either calculate the area as part of the constructor, then store that value, or don't store the value at all, and always call caculateArea() to get the area.

hunter9000a at 2007-7-9 23:21:46 > top of Java-index,Java Essentials,New To Java...