Source of OverloadedMethods.java


  1: // Fig. 18.1: OverloadedMethods.java
  2: // Using overloaded methods to print array of different types.
  3: 
  4: public class OverloadedMethods 
  5: {
  6:    // method printArray to print Integer array
  7:    public static void printArray( Integer[] inputArray )
  8:    {
  9:       // display array elements
 10:       for ( Integer element : inputArray )
 11:          System.out.printf( "%s ", element );
 12: 
 13:       System.out.println();
 14:    } // end method printArray
 15: 
 16:    // method printArray to print Double array
 17:    public static void printArray( Double[] inputArray )
 18:    {
 19:       // display array elements
 20:       for ( Double element : inputArray )
 21:          System.out.printf( "%s ", element );
 22: 
 23:       System.out.println();
 24:    } // end method printArray
 25: 
 26:    // method printArray to print Character array
 27:    public static void printArray( Character[] inputArray )
 28:    {
 29:       // display array elements
 30:       for ( Character element : inputArray )
 31:          System.out.printf( "%s ", element );
 32: 
 33:       System.out.println();
 34:    } // end method printArray
 35: 
 36:    public static void main( String args[] ) 
 37:    {
 38:       // create arrays of Integer, Double and Character
 39:       Integer[] integerArray = { 1, 2, 3, 4, 5, 6 };
 40:       Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
 41:       Character[] characterArray = { 'H', 'E', 'L', 'L', 'O' };
 42: 
 43:       System.out.println( "Array integerArray contains:" );
 44:       printArray( integerArray ); // pass an Integer array
 45:       System.out.println( "\nArray doubleArray contains:" );
 46:       printArray( doubleArray ); // pass a Double array
 47:       System.out.println( "\nArray characterArray contains:" );
 48:       printArray( characterArray ); // pass a Character array
 49:    } // end main
 50: } // end class OverloadedMethods
 51: 
 52: /**************************************************************************
 53:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 54:  * Pearson Education, Inc. All Rights Reserved.                           *
 55:  *                                                                        *
 56:  * DISCLAIMER: The authors and publisher of this book have used their     *
 57:  * best efforts in preparing the book. These efforts include the          *
 58:  * development, research, and testing of the theories and programs        *
 59:  * to determine their effectiveness. The authors and publisher make       *
 60:  * no warranty of any kind, expressed or implied, with regard to these    *
 61:  * programs or to the documentation contained in these books. The authors *
 62:  * and publisher shall not be liable in any event for incidental or       *
 63:  * consequential damages in connection with, or arising out of, the       *
 64:  * furnishing, performance, or use of these programs.                     *
 65:  *************************************************************************/