Create a Class called MyMathClass The class handles bound type parameters that e
ID: 3737648 • Letter: C
Question
Create a Class called MyMathClass
The class handles bound type parameters that extend the API Number class. See Bound for Type Parameters(around pg 793 in text)
average method
takes in a type parameter ArrayList<T>
returns a double representing the average of the ArrayList elements
standardDeviation method
that takes in a type parameter ArrayList<T>
returns a double representing the Standard Deviation of the ArrayList elements.
Standard Deviation Equation
Sqrt of the sum of the difference between each element in the array and the average of all elements in the array.
Standard Deviation = Sqrt( (Sum_i=1_to_N(element i - average)) / N) where N is the number of elements in the array.
Create a Class classed MyMathClassTester
Creates three ArrayLists, one for each type of numeric Object Type(Integer, Double and Long)(you must use an L fpr any number literal to a Long before passing it to the List)(example 2348575758390L)
Add at least three appropriate number to each ArrayList
Create a MyMathClass object for each type (Integer, Double and Long)
Call the Standard Deviation method for each MyMathClass object and display the results.
What happens if you create a MyMathClass object with a String type parameter?
Add the answer to the above question as comments at the bottom of your MyMathClassTester
Explanation / Answer
Hello, I have created the required MyMathClass.java with generic Type extending Number class. And defined the required methods average() and standardDeviation() as needed, explained by javadoc comments. Created a test class MyMathClassTester and defined three array lists of Integer, Double and Long types. Added values to each lists, and defined three MyMathClass instances of Integer, Double and Long types and displayed the standard deviation values.
Note: The equation for finding standard deviation was not correct. So I have corrected it too. Thank you.
// MyMathClass.java
import java.util.ArrayList;
public class MyMathClass<T extends Number> {
/**
* takes in a type parameter ArrayList<T> returns a double representing the
* average of the ArrayList elements
*/
public double average(ArrayList<T> list) {
double total = 0.0;
for (T item : list) {
/**
* getting the double value of the current item and adding to the
* total
*/
total += item.doubleValue();
}
/**
* average=total/count
*/
return total / list.size();
}
/**
* takes in a type parameter ArrayList<T> returns a double representing the
* Standard Deviation of the ArrayList elements.
*
*/
public double standardDeviation(ArrayList<T> list) {
double average = average(list);// finding the average
/**
* variable to store the squares of sum of differences between each
* element and the average
*/
double sumOfDifferenceSquares = 0;
for (T item : list) {
/**
* finding the difference of current item and average
*/
double difference = Math.abs(item.doubleValue() - average);
/**
* finding the square and adding to the sumOfDifferenceSquares
*/
sumOfDifferenceSquares += difference * difference;
}
/**
* finding the average value of squares of sum of differences
*/
double averageOfSumDifferenceSquares = (double) sumOfDifferenceSquares
/ list.size();
/**
* standard deviation=Square root of average of squares of sum of
* differences
*/
double sd = Math.sqrt(averageOfSumDifferenceSquares);
return sd;
}
}
// MyMathClassTester.java
import java.util.ArrayList;
public class MyMathClassTester {
public static void main(String[] args) {
/**
* Defining array lists of Integer, Double and Long types
*/
ArrayList<Integer> integers = new ArrayList<Integer>();
ArrayList<Double> doubles = new ArrayList<Double>();
ArrayList<Long> longIntegers = new ArrayList<Long>();
/**
* Adding appropriate values to each lists
*/
integers.add(53);
integers.add(46);
integers.add(79);
doubles.add(123.5596);
doubles.add(22389.5596);
doubles.add(906256.5596);
longIntegers.add(2147483649L);
longIntegers.add(122349673L);
longIntegers.add(12345408L);
/**
* Defining MyMathClass instances of Integer, Double and Long types
*/
MyMathClass<Integer> integerMathClass = new MyMathClass<Integer>();
MyMathClass<Double> doubleMathClass = new MyMathClass<Double>();
MyMathClass<Long> longMathClass = new MyMathClass<Long>();
/**
* We cannot create a MyMathClass of String type, it will throw a
* compile error as String doesn't extend the Number class
*/
/**
* Displaying the standard deviations of each lists
*/
System.out.println("standard deviation of integer values: "
+ integerMathClass.standardDeviation(integers));
System.out.println("standard deviation of double values: "
+ doubleMathClass.standardDeviation(doubles));
System.out.println("standard deviation of long values: "
+ longMathClass.standardDeviation(longIntegers));
}
}
/*OUTPUT*/
standard deviation of integer values: 14.197026292697903
standard deviation of double values: 422004.95812437515
standard deviation of long values: 9.816134036058714E8