Write a generic class with a type parameter constrained to the Number class or a
ID: 3757787 • Letter: W
Question
Write a generic class with a type parameter constrained to the Number class or any subclass of Number. The constructor should accept an array of 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.
I need help getting this code up and running with the code in the photo. If someone can help me out, I will appreciate it.
It also needs tons of comments and should not have a max size of an array
Explanation / Answer
package mani;
import java.util.Scanner;
public class Analyzer <T extends Number>
{
T[] array;
//constructor.
public Analyzer (T[] a)
{
array = a;
} //end constructor
/*
*highest method returns the
*highest value in the array.
*/
T getHighest()
{
T high = array[0];
//find the highest value.
for (int i = 1; i <array.length; i++)
{
if ((high.toString()). compareTo(array[i].toString())<0)
{
high = array[i];
}
}
return high;
} //end of constructor
public T getLowest()
{
T low = array[0];
//find the highest value.
for (int i = 1; i <array.length; i++)
{
if ((low.toString()). compareTo(array[i].toString())>0)
{
low= array[i];
}
}
return low;
} //end of constructor
public T getTotal()
{
T total = array[0];
//find the highest value.
for (int i = 1; i <array.length; i++)
{
int k=Integer.parseInt(total.toString());
int arr=Integer.parseInt(array[i].toString());
int tot=k+arr;
Integer in=new Integer(tot);
total=(T) in;
}
return total;
}
public T getAverage()
{
T total = array[0];
//find the highest value.
for (int i = 1; i <array.length; i++)
{
int k=Integer.parseInt(total.toString());
int arr=Integer.parseInt(array[i].toString());
int tot=k+arr;
Integer in=new Integer(tot);
total=(T) in;
}
int k=Integer.parseInt(total.toString());
k=k/array.length;
Integer in=new Integer(k);
total=(T) in;
return total;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.