JAVA Link from instruction httpenwikipediaorgwikiStandarddev
JAVA
Link from instruction
http://en.wikipedia.org/wiki/Standard_deviation
Code from instruction
public class stdev {
public static void main(String[] args) {
double [] d = new double[10];
for(int i = 1; i <=10; i++)
d[i-1]=i;
double total = 0;
double average = 0;
for(int i = 0; i < 10; i++)
total += d[i];
average = total/10;
System.out.println(\"average \"+average);
total = 0;
for(int i = 0; i < 10; i++)
total += (d[i] - average)*(d[i]-average);
total /= 9;
double std = Math.sqrt(total);
System.out.println(\"Stdev = \" + std);
}
}
Write a generic class MyMathClass with at type parameter T where Tis a numeric object (Integer, Double or any class that extends jayalang-number) Add a method standardDeviation (stdev) that takes an ArrayList of type Tand 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 KT extends Number public static KT extends Number double stdev (ArrayList a) Research java\' s Number class to see what useful method we are gaining access to Standard Deviation is the average amount of deviation from the average See http://en wikipedia.org/wiki/Standard deviation The formula that must be implemented is N ari i-1 Where the x with a bar over it is the average. The greek letter sigma stands for a summation So you need to take each element in the array, subtract the average from it, square it and add this number to a tota After you take the total divide by the number of elements 1 and take the square root. Example Tf the input is 1 2 3 4 5 6 7 8 9 10. The average is 5.5 The result for the total would be: 5.5 5.5) (2 5.5) (3 5.5 3-5.5 (10 5.5 10 5.5Solution
Hi, I have implemented program.
Please let me know in case of any issue.
import java.util.ArrayList;
public class MyMathClass<T extends Number> {
public static <T extends Number> double standardDeviation(ArrayList<T> list){
double total = 0;
double average = 0;
for(T x : list)
total = total + x.doubleValue();
average = total/10;
// System.out.println(\"average \"+average);
total = 0;
for(T x : list){
double d = x.doubleValue();
total += (d - average)*(d-average);
}
total /= 9;
double std = Math.sqrt(total);
return std;
}
public static void main(String[] args) {
ArrayList<Double> dList = new ArrayList<>();
ArrayList<Integer> iList = new ArrayList<>();
for(int i=1; i<=10; i++){
dList.add((double) i);
iList.add(i);
}
double dDev = MyMathClass.standardDeviation(dList);
double iDev = MyMathClass.standardDeviation(iList);
System.out.println(\"standard deviation 0-9 type Double \"+dDev);
System.out.println(\"standard deviation 0-9 type Integer \"+iDev);
}
}
/*
Sample output:
standard deviation 0-9 type Double 3.0276503540974917
standard deviation 0-9 type Integer 3.0276503540974917
*/
![JAVA Link from instruction http://en.wikipedia.org/wiki/Standard_deviation Code from instruction public class stdev { public static void main(String[] args) { d JAVA Link from instruction http://en.wikipedia.org/wiki/Standard_deviation Code from instruction public class stdev { public static void main(String[] args) { d](/WebImages/35/java-link-from-instruction-httpenwikipediaorgwikistandarddev-1104889-1761584570-0.webp)
![JAVA Link from instruction http://en.wikipedia.org/wiki/Standard_deviation Code from instruction public class stdev { public static void main(String[] args) { d JAVA Link from instruction http://en.wikipedia.org/wiki/Standard_deviation Code from instruction public class stdev { public static void main(String[] args) { d](/WebImages/35/java-link-from-instruction-httpenwikipediaorgwikistandarddev-1104889-1761584570-1.webp)
![JAVA Link from instruction http://en.wikipedia.org/wiki/Standard_deviation Code from instruction public class stdev { public static void main(String[] args) { d JAVA Link from instruction http://en.wikipedia.org/wiki/Standard_deviation Code from instruction public class stdev { public static void main(String[] args) { d](/WebImages/35/java-link-from-instruction-httpenwikipediaorgwikistandarddev-1104889-1761584570-2.webp)