Number Analyzer: Write a generic class with a type parameter constrained to the
ID: 3759021 • Letter: N
Question
Number Analyzer: 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. Demonstrate the class in an applicaiton.
// TODO this type parameter must be constrained (see textbook)
public class Analyzer<T> {
// TODO add field(s)
public Analyzer(T[] values) {
// TODO implement
}
public T getHighest() {
// TODO implement
return null;
}
public T getLowest() {
// TODO implement
return null;
}
public double getTotal() {
// TODO implement
return 0.0;
}
public double getAverage() {
// TODO implement
return 0.0;
}
public static void main(String[] args) {
Integer[] numbers = {44, 77, 11, 99, 55, 22};
// Create an Analyzer object.
Analyzer<Integer> analyzer = new Analyzer<>(numbers);
// Display the highest value in the array.
System.out.println("The highest value is " +
analyzer.getHighest());
// Display the lowest value in the array.
System.out.println("The lowest value is " +
analyzer.getLowest());
// Display the total of the values in the array.
System.out.println("The total of the values is " +
analyzer.getTotal());
// Display the average of the values in the array.
System.out.println("The average of the values is " +
analyzer.getAverage());
}
}
Explanation / Answer
//Analyzer.java
//The class Analyzer that extends the Number class
public class Analyzer<T extends Number>
{
//create an generic array T
T[] objects;
public Analyzer(T[] values)
{
//set values
objects = values;
}
/*The method getHighest returns the maximum value**/
public T getHighest()
{
//Assume first element is maximum
T max=objects[0];
for (int index = 1 ; index < objects.length; index++)
{
//check for maximum value
if(objects[index].doubleValue()>max.doubleValue())
max=objects[index];
}
return max;
}
/*The method getLowest returns the minimum value**/
public T getLowest()
{
//assume first element is minimum
T min=objects[0];
//check for miimum value
for (int index = 1 ; index < objects.length; index++)
{
if(objects[index].doubleValue()<min.doubleValue())
min=objects[index];
}
return min;
}
/*The method getTotal returns
*the total of generic type values*/
public double getTotal()
{
//set total to zero
double total =0;
for (T number : objects)
{
//add number to total value
total += number.doubleValue();
}
return total;
}
/*The method getAverage returns
*the average of generic type values*/
public double getAverage()
{
//set total zero
double total =0;
for (T number : objects)
{
total += number.doubleValue();
}
//return average of total.
return total/objects.length;
}
public static void main(String[] args)
{
//Create an array of Integer
Integer[] numbers = {44, 77, 11, 99, 55, 22};
// Create an Analyzer object.
Analyzer<Integer> analyzer = new Analyzer<Integer>(numbers);
// Display the highest value in the array.
System.out.println("The highest value is " +
analyzer.getHighest());
// Display the lowest value in the array.
System.out.println("The lowest value is " +
analyzer.getLowest());
// Display the total of the values in the array.
System.out.println("The total of the values is " +
analyzer.getTotal());
// Display the average of the values in the array.
System.out.println("The average of the values is " +
analyzer.getAverage());
}//end of the main method
}//end of the class Analyzer
------------------------------------------------------------------------------------------------------------------
Sample Output:
The highest value is 99
The lowest value is 11
The total of the values is 308.0
The average of the values is 51.333333333333336
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.