Web and email hosting - Chapter 6 Methods 289 1 // Fig. 6.16:
Chapter 6 Methods 289 1 // Fig. 6.16: MethodOverload.java 2 // Using overloaded methods 3 4 // Java core packages 5 import java.awt.Container; 6 7 // Java extension packages 8 import javax.swing.*; 9 10 public class MethodOverload extends JApplet { 11 12 // set up GUI and call versions of method square 13 public void init() 14 { 15 JTextArea outputArea = new JTextArea(); 16 Container container = getContentPane(); 17 container.add( outputArea ); 18 19 outputArea.setText( 20 “The square of integer 7 is ” + square( 7 ) + 21 “nThe square of double 7.5 is ” + square( 7.5 ) ); 22 } 23 24 // square method with int argument 25 public int square( int intValue ) 26 { 27 System.out.println( 28 “Called square with int argument: ” + intValue ); 29 30 return intValue * intValue; 31 32 } // end method square with int argument 33 34 // square method with double argument 35 public double square( double doubleValue ) 36 { 37 System.out.println( 38 “Called square with double argument: ” + doubleValue ); 39 40 return doubleValue * doubleValue; 41 42 } // end method square with double argument 43 44 } // end class MethodOverload Fig. 6.16Using overloaded methods (part 1 of 2). Fig. 6.16 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01