Chapter 9 Object-Oriented Programming 491 21 (How to cite a web site) 22 setHeight(

Chapter 9 Object-Oriented Programming 491 21 22 setHeight( cylinderHeight ); 23 } 24 25 // set height of Cylinder 26 public void setHeight( double cylinderHeight ) 27 { 28 height = ( cylinderHeight >= 0 ? cylinderHeight : 0 ); 29 } 30 31 // get height of Cylinder 32 public double getHeight() 33 { 34 return height; 35 } 36 37 // calculate area of Cylinder (i.e., surface area) 38 public double area() 39 { 40 return 2 * super.area() + 2 * Math.PI * radius * height; 41 } 42 43 // calculate volume of Cylinder 44 public double volume() 45 { 46 return super.area() * height; 47 } 48 49 // convert Cylinder to a String representation 50 public String toString() 51 { 52 return super.toString() + “; Height = ” + height; 53 } 54 55 // return shape name 56 public String getName() 57 { 58 return “Cylinder”; 59 } 60 61 } // end class Cylinder Fig. 9.25 Cylindersubclass of Circle indirect subclass of abstract class Shape(part 2 of 2). Method mainof class Test(Fig. 9.26) instantiates Pointobject point, Circle object circleand Cylinderobject cylinder(lines 16 18). Next, array arrayOf- Shapes is instantiated (line 21). This array of superclass Shape references will refer to each subclass object instantiated. At line 24, the reference pointis assigned to array element arrayOfShapes[0]. At line 27, the reference circleis assigned to array element arrayOfShapes[1]. At line 30, the reference cylinder is assigned to array element arrayOfShapes[2]. Now, each superclass Shape reference in the array refers to a subclass object of type Point, Circleor Cylinder. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply