public class MaximumTest
1: // Fig. 18.5: MaximumTest.java
2: // Generic method maximum returns the largest of three objects.
3:
4: public class MaximumTest
5: {
6: // determines the largest of three Comparable objects
7: public static < T extends Comparable< T > > T maximum( T x, T y, T z )
8: {
9: T max = x; // assume x is initially the largest
10:
11: if ( y.compareTo( max ) > 0 )
12: max = y; // y is the largest so far
13:
14: if ( z.compareTo( max ) > 0 )
15: max = z; // z is the largest
16:
17: return max; // returns the largest object
18: } // end method maximum
19:
20: public static void main( String args[] )
21: {
22: System.out.printf( "Maximum of %d, %d and %d is %d\n\n", 3, 4, 5,
23: maximum( 3, 4, 5 ) );
24: System.out.printf( "Maximum of %.1f, %.1f and %.1f is %.1f\n\n",
25: 6.6, 8.8, 7.7, maximum( 6.6, 8.8, 7.7 ) );
26: System.out.printf( "Maximum of %s, %s and %s is %s\n", "pear",
27: "apple", "orange", maximum( "pear", "apple", "orange" ) );
28: } // end main
29: } // end class MaximumTest
30:
31:
32: /**************************************************************************
33: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
34: * Pearson Education, Inc. All Rights Reserved. *
35: * *
36: * DISCLAIMER: The authors and publisher of this book have used their *
37: * best efforts in preparing the book. These efforts include the *
38: * development, research, and testing of the theories and programs *
39: * to determine their effectiveness. The authors and publisher make *
40: * no warranty of any kind, expressed or implied, with regard to these *
41: * programs or to the documentation contained in these books. The authors *
42: * and publisher shall not be liable in any event for incidental or *
43: * consequential damages in connection with, or arising out of, the *
44: * furnishing, performance, or use of these programs. *
45: *************************************************************************/