Source of ItemMinimum.java


  1: //ItemMinimum.java

  3: public class ItemMinimum
  4: {
  5:     // Find the minimum of three **ints**
  6:     // Done the "old-fashioned" way
  7:     public static Integer tripleMinInt
  8:     (
  9:         Integer item1,
 10:         Integer item2,
 11:         Integer item3
 12:     )
 13:     {
 14:         Integer minVal;

 16:         minVal = item1;

 18:         if (item2.compareTo(minVal) < 0)
 19:         {
 20:             minVal = item2;
 21:         }
 22:         if (item3.compareTo(minVal) < 0)
 23:         {
 24:             minVal = item3;
 25:         }
 26:         return minVal;
 27:     }

 29:     // Find the minimum of three **chars**
 30:     // Done the "old-fashioned" way
 31:     public static Character tripleMinChar
 32:     (
 33:         Character item1,
 34:         Character item2,
 35:         Character item3
 36:     )
 37:     {
 38:         Character minVal;

 40:         minVal = item1;

 42:         if (item2.compareTo(minVal) < 0)
 43:         {
 44:             minVal = item2;
 45:         }
 46:         if (item3.compareTo(minVal) < 0)
 47:         {
 48:             minVal = item3;
 49:         }
 50:         return minVal;
 51:     }

 53:     //Find the minimum of three values of any comparable type
 54:     //Done the "modern" way
 55:     public static <TheType extends Comparable<TheType>>
 56:     TheType tripleMin
 57:     (
 58:         TheType item1,
 59:         TheType item2,
 60:         TheType item3
 61:     )
 62:     {
 63:         TheType minVal = item1; // Holds min item value, init to first item

 65:         if (item2.compareTo(minVal) < 0)
 66:         {
 67:             minVal = item2;
 68:         }
 69:         if (item3.compareTo(minVal) < 0)
 70:         {
 71:             minVal = item3;
 72:         }
 73:         return minVal;
 74:     }

 76:     public static void main(String[] args)
 77:     {
 78:         Integer num1 = 66;    // Test case 1, item1
 79:         Integer num2 = 99;    // Test case 1, item2
 80:         Integer num3 = 55;    // Test case 1, item3

 82:         Character let1 = 'z'; // Test case 2, item1
 83:         Character let2 = 'a'; // Test case 2, item2
 84:         Character let3 = 'm'; // Test case 2, item3

 86:         String str1 = "zzz";  // Test case 3, item1
 87:         String str2 = "aaa";  // Test case 3, item2
 88:         String str3 = "mmm";  // Test case 3, item3

 90:         // Try tripleMin method with Integers
 91:         System.out.println("Items: " + num1 + " " + num2 + " " + num3);
 92:         System.out.println("Min: " + tripleMin(num1, num2, num3));

 94:         //Using the old-fashioned method ...
 95:         System.out.println
 96:         (
 97:             "Min: " + tripleMinInt(num1, num2, num3) + "\n"
 98:         );

100:         // Try tripleMin method with Characters
101:         System.out.println("Items: " + let1 + " " + let2 + " " + let3);
102:         System.out.println("Min: " + tripleMin(let1, let2, let3));

104:         //Using the old-fashioned method ...
105:         System.out.println
106:         (
107:             "Min: " + tripleMinChar(let1, let2, let3) + "\n"
108:         );

110:         // Try tripleMin method with Strings
111:         System.out.println("Items: " + str1 + " " + str2 + " " + str3);
112:         System.out.println("Min: " + tripleMin(str1, str2, str3) + "\n");
113:     }
114: }