Source of IntSumWithoutWildcard.java


  1: //IntSumWithoutWildcard.java
  2: //Will not compile with IntSumWithoutWildcardDemo.java because of the
  3: //generic T parameter in line 30. Motivates the "wildcard".

  5: //Question being asked here:
  6: //Is the "integer sum" of the values in the T array the same, no
  7: //matter what kind of (numeric, of course) values are in the array?
  8: public class IntSumWithoutWildcard<T extends Number>
  9: {
 10:     private T[] values;

 12:     public IntSumWithoutWildcard(T[] a)
 13:     {
 14:         values = a;
 15:     }

 17:     //Return an integer sum in all cases.
 18:     public int intSum()
 19:     {
 20:         double sum = 0.0;
 21:         for (int i = 0; i < values.length; i++)
 22:         {
 23:             sum += values[i].doubleValue();
 24:         }
 25:         return (int) sum;
 26:     }

 28:     //Return an integer sum in all cases.
 29:     public boolean hasSameIntSum(IntSumWithoutWildcard<T> obj)
 30:     {
 31:         if (this.intSum() == obj.intSum()) //"this" is redundant here
 32:         {
 33:             return true;
 34:         }
 35:         return false;
 36:     }
 37: }