468 Object-Oriented Programming Chapter 9 (Yahoo web space) 15 // constructor
468 Object-Oriented Programming Chapter 9 15 // constructor 16 public Circle( double circleRadius, int xCoordinate, 17 int yCoordinate ) 18 { 19 // call superclass constructor to set coordinates 20 super( xCoordinate, yCoordinate ); 21 22 // set radius 23 setRadius( circleRadius ); 24 } 25 26 // set radius of Circle 27 public void setRadius( double circleRadius ) 28 { 29 radius = ( circleRadius >= 0.0 ? circleRadius : 0.0 ); 30 } 31 32 // get radius of Circle 33 public double getRadius() 34 { 35 return radius; 36 } 37 38 // calculate area of Circle 39 public double area() 40 { 41 return Math.PI * radius * radius; 42 } 43 44 // convert the Circle to a String 45 public String toString() 46 { 47 return “Center = ” + “[” + x + “, ” + y + “]” + 48 “; Radius = ” + radius; 49 } 50 51 } // end class Circle Fig. 9.12 Circleclass definition (part 2 of 2). Application Test (Fig. 9.13) instantiates an object of class Circle (line 19), then uses get methods to obtain the information about the Circleobject. Method mainindirectly references the protected data of class Circle through method calls. Method main then uses set methods setRadiusand setPoint to reset the radius and coordinates of the center of the circle. Finally, main displays the Circle object circle and calculates and displays its area. 1 // Fig. 9.13: Test.java 2 // Applet to test class Circle 3 Fig. 9.13 Testing class Circle(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01