Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A generic class with a type parameter constrained to the number class or any sub

ID: 3634014 • Letter: A

Question

A generic class with a type parameter constrained to the number class or any subclass of number. the constructor should accept an array such objects. the class should have methods that return the highest and lowest values in the array the total of the elements, and the average value of all the elements. demonstrate the class in an application

Heres what I have.

/**
The numberAnalyzer class takes a array of numbers
and find the number of elements in the array. The
highest and lowest number found in the array and the
average.
*/

public class NumberAnalyzer <T>
{
private T highest; //highest number.
private T lowest; //lowest number.
private T average; //average number.
private T totalElements; //total number of elements.
int[] numbers={5,9,1,22,90}; //array to hold numbers

/**
Constructor
@param high the highest
@param low the lowest
@param avg the average
@param total elements
*/

public NumberAnalyzer(T high, T low, T avg, T total)
{
highest=high;
lowest=low;
average=avg;
totalElements=total;
}
/**
setHigh method finds the highest number in the array.
@param high The value of the highest number.
*/

public int getHigh()
{

int high=0;
for(int index=1;index<numbers.length;index++)
{
if(numbers[index]>high)
high=numbers[index];

}
return high;
}

public int getLow()
{

int low=0;
for(int index=1;index<numbers.length;index++)
{
if(numbers[index]<low)
low=numbers[index];

}
return low;
}

public int getAverage()
{

int avg=0;
avg=(getHigh()+getLow())/2;
return avg;
}


public int getTotal()
{

int elements=0;
for(int index=1;index<numbers.length;index++)
{
if(numbers[index]>high)
high=numbers[index];

}
return index;
}
}
System.out.println("Highest number is " +high+ "n/ Lowest number is "+low+
"n/Average of numbers is " +avg+ "n/ Total number of elements is "+index);
}
}

Explanation / Answer

//GenericNumber.java
public class GenericNumber<E extends Number & Comparable<E>>
{
   private E[] _numbers;
   public GenericNumber(E[] input)
   {
       _numbers = input;
   }
  
   public E getHighest()
   {
       if(_numbers.length > 0)
       {
           E maximum = _numbers[0];
           for(int i=1;i<_numbers.length;i++)
           {
               if(maximum.compareTo(_numbers[i]) < 0)
               {
                   maximum = _numbers[i];
               }
           }
           return maximum;
       }
       return (E) null;
   }
  
   public E getLowest()
   {
       if(_numbers.length > 0)
       {
           E minimun = _numbers[0];
           for(int i=1;i<_numbers.length;i++)
           {
               if(minimun.compareTo(_numbers[i]) > 0)
               {
                   minimun = _numbers[i];
               }
           }
           return minimun;
       }
       return (E) null;
   }
  
   @SuppressWarnings("unchecked")
   public E getTotal()
   {
        Double sum = 0.0;
       for(int i=1;i<_numbers.length;i++)
       {
           sum = sum + Double.parseDouble(_numbers[i].toString());
       }
       return (E)sum;
   }
  
   @SuppressWarnings("unchecked")
   public E getAverage()
   {
       Double avg = ((Double)getTotal() / _numbers.length);
       return (E)avg ;
   }
}

//TestGenricNumbers.java
public class TestGenricNumbers
{
    public static void main(String[] args)
    {
        Integer[] numbers = {1,5,3,8,10,24,12};
        GenericNumber<Integer> arrNum = new GenericNumber<Integer>(numbers);
       
        System.out.println("Heighest of the array : " + arrNum.getHighest());
        System.out.println("Lowest of the array : " + arrNum.getLowest());
        System.out.println("Total of the array : " + arrNum.getTotal());
        System.out.println("Average of the array : " + arrNum.getAverage());
       
        Double[] dnumbers = {1.0,5.0,3.0,8.0,10.0,24.0,12.0};
        GenericNumber<Double> arrNums = new GenericNumber<Double>(dnumbers);
       
        System.out.println("Heighest of the array : " + arrNums.getHighest());
        System.out.println("Lowest of the array : " + arrNums.getLowest());
        System.out.println("Total of the array : " + arrNums.getTotal());
        System.out.println("Average of the array : " + arrNums.getAverage());
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote