Web server iis - 492 Object-Oriented Programming Chapter 9 1 // Fig.

492 Object-Oriented Programming Chapter 9 1 // Fig. 9.26: Test.java 2 // Class to test Shape, Point, Circle, Cylinder hierarchy 3 4 // Java core packages 5 import java.text.DecimalFormat; 6 7 // Java extension packages 8 import javax.swing.JOptionPane; 9 10 public class Test { 11 12 // test Shape hierarchy 13 public static void main( String args[] ) 14 { 15 // create shapes 16 Point point = new Point( 7, 11 ); 17 Circle circle = new Circle( 3.5, 22, 8 ); 18 Cylinder cylinder = new Cylinder( 10, 3.3, 10, 10 ); 19 20 // create Shape array 21 Shape arrayOfShapes[] = new Shape[ 3 ]; 22 23 // aim arrayOfShapes[ 0 ] at subclass Point object 24 arrayOfShapes[ 0 ] = point; 25 26 // aim arrayOfShapes[ 1 ] at subclass Circle object 27 arrayOfShapes[ 1 ] = circle; 28 29 // aim arrayOfShapes[ 2 ] at subclass Cylinder object 30 arrayOfShapes[ 2 ] = cylinder; 31 32 // get name and String representation of each shape 33 String output = 34 point.getName() + “: ” + point.toString() + “n” + 35 circle.getName() + “: ” + circle.toString() + “n” + 36 cylinder.getName() + “: ” + cylinder.toString(); 37 38 DecimalFormat precision2 = new DecimalFormat( “0.00″ ); 39 40 // loop through arrayOfShapes and get name, 41 // area and volume of each shape in arrayOfShapes 42 for ( int i = 0; i < arrayOfShapes.length; i++ ) { 43 output += “nn” + arrayOfShapes[ i ].getName() + 44 “: ” + arrayOfShapes[ i ].toString() + 45 “nArea = ” + 46 precision2.format( arrayOfShapes[ i ].area() ) + 47 “nVolume = ” + 48 precision2.format( arrayOfShapes[ i ].volume() ); 49 } 50 51 JOptionPane.showMessageDialog( null, output, 52 “Demonstrating Polymorphism”, 53 JOptionPane.INFORMATION_MESSAGE ); Fig. 9.26 Shape, Point, Circle, Cylinderhierarchy (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply