Language: JAVA(Netbeans)
Write a generic class MyMathClass with at type parameter T where T is a numeric object (Integer, Double or any class that extends java.lang.number) Add a method standardDeviation (stdev) that takes an ArrayList of type T and returns a standard deviation as type double. Use a for each loop where appropriate.
Hard code a couple of test arrays into your Demo file. You must use at least 2 different types such as Double and Integer.
Your call will be something like:
System.out.println(“Standard Deviation 0-9 “ + MyMathClass.stdev(a));
Your class and method headers will be:
public class MyMathClass<T extends Number> {
public static <T extends Number> double stdev(ArrayList<T> a){
Research java’s Number class to see what useful method we are gaining access to.
Here is the completed code for this problem including a Demo test program. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Make sure you copy below two classes into separate files as mentioned. Do not copy everything to a single file.
//MyMathClass.java
import java.util.ArrayList;
public class MyMathClass<T extends Number> {
// required method
public static <T extends Number> double stdev(ArrayList<T> a) {
double sum = 0, mean = 0;
// finding sum of values in list a
for (T number : a) {
// using doubleValue() method to get value of number as a double,
// note that this may lead to precision issues if the size of type
// is more than what double can hold. assuming it is under limit.
sum += number.doubleValue();
}
// finding mean
mean = (double) sum / a.size();
// initializing sum of squared differences with mean to 0
double sum_squared_diff = 0;
//looping through each number again
for (T number : a) {
//finding difference between number and mean
double diff = number.doubleValue() - mean;
//adding the square of diff to sum_squared_diff
sum_squared_diff += (diff * diff);
}
//dividing sum_squared_diff by number of elements to get variance
double variance = (double) sum_squared_diff / a.size();
//square root of variance is standard deviation
double sd = Math.sqrt(variance);
return sd;
}
}
//Demo.java
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
// creating an integer array list, adding some values and testing
// MyMathClass.stdev() method
ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(1);
iList.add(2);
iList.add(3);
iList.add(4);
iList.add(5);
System.out.println("Integer list: " + iList);
System.out.println("Standard deviation: " + MyMathClass.stdev(iList));
// creating a double array list, adding some values and testing
// MyMathClass.stdev() method
ArrayList<Double> dList = new ArrayList<Double>();
dList.add(12.5);
dList.add(22.2);
dList.add(31.3);
dList.add(42.9);
dList.add(53.8);
System.out.println("Double list: " + dList);
System.out.println("Standard deviation: " + MyMathClass.stdev(dList));
}
}
/*OUTPUT*/
Integer list: [1, 2, 3, 4, 5]
Standard deviation: 1.4142135623730951
Double list: [12.5, 22.2, 31.3, 42.9, 53.8]
Standard deviation: 14.624445288625479
Get Answers For Free
Most questions answered within 1 hours.