Java Reference
In-Depth Information
33
/** Return height */
34
public double getHeight() {
35
return height;
36 }
37
38
/** Set a new height */
39
public void setHeight( double height) {
40
this .height = height;
41 }
42
43
/** Return area */
44
public double getArea() {
45
return width * height;
46 }
47
48
/** Return perimeter */
49
public double getPerimeter() {
50
return 2 * (width + height);
51 }
52 }
The code in Listing 11.4 creates objects of Circle and Rectangle and invokes the methods
on these objects. The toString() method is inherited from the GeometricObject class
and is invoked from a Circle object (line 5) and a Rectangle object (line 13).
L ISTING 11.4 TestCircleRectangle.java
1 public class TestCircleRectangle {
2
public static void main(String[] args) {
3
4
5 System.out.println( "A circle " +
CircleFromSimpleGeometricObject circle =
new CircleFromSimpleGeometricObject( 1 );
Circle object
invoke toString
invoke getColor
circle.toString()
);
circle.getColor()
6 System.out.println( "The color is " +
);
7 System.out.println( "The radius is " +
circle.getRadius()
);
circle.getArea()
8 System.out.println( "The area is " +
);
9 System.out.println( "The diameter is " +
circle.getDiameter()
);
10
11
12
13 System.out.println( "\nA rectangle " +
RectangleFromSimpleGeometricObject rectangle =
new RectangleFromSimpleGeometricObject( 2 , 4 );
Rectangle object
invoke toString
rectangle.toString()
);
rectangle.getArea()
14 System.out.println( "The area is " +
);
15 System.out.println( "The perimeter is " +
16
rectangle.getPerimeter()
);
17 }
18 }
A circle created on Thu Feb 10 19:54:25 EST 2011
color: white and filled: false
The color is white
The radius is 1.0
The area is 3.141592653589793
The diameter is 2.0
A rectangle created on Thu Feb 10 19:54:25 EST 2011
color: white and filled: false
The area is 8.0
The perimeter is 12.0
 
Search WWH ::




Custom Search