Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

** Must use ele.thatmethod() to extract the double value from type T ** ** In Ja

ID: 3707308 • Letter: #

Question

** Must use ele.thatmethod() to extract the double value from type T **

** In Java with detailed comments **

=======================================================================

Feel free to manipulate the code below or start from scratch

** Must use ele.thatmethod() to extract the double value from type T **

** In Java with detailed comments **

** It has to have a method from number that extracts the double value. Look up the Number class in Java online then use ele.thatmethod() to extract the double value from type T. **

========================================================================

import java.util.ArrayList;

public class MyMathClass

{

public static double stdev(ArrayList a)

{

double avg = 0;

// Stores average of numbers

for(T ele : a)

// Loops through all the elements in the list

{

avg = avg + (double)ele;

// Adds the number to the average

}

avg /= a.size();

// Divides the average

System.out.println("Average = " + avg);

double stdDev = 0.0;

// Stores standard deviation

for(T ele : a) {

// Adds the term (x-mean)^2 to stdDev

stdDev += ((double)(ele) - avg) * ((double)(ele) - avg);

}

// Divide stdDev by (N-1) and take the square root

stdDev = Math.sqrt(stdDev/(a.size() - 1));

return stdDev;

}

public static void main(String args[])

{

ArrayList d = new ArrayList<>();

for(int i=1; i<=10; i++)

{

d.add((double) i);

}

System.out.println("Stdev = " + MyMathClass.stdev(d));

System.out.println(" Process completed.");

}

}

Assignment 9 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 Your class and method headers will be public class MyMathclass MyMathClass.stdev (a)) public static 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 len.wikipedia ndard deviation The formula that must be implemented is 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 total. After you take the total divide by the number of elements 1 and take the square root. Example If the Input is123456789 10 The averago is 5s The result fox the total would be: 1-5.5) (2-5.5) +(2-5.5) (2-5.5) +(3-5.5) (3-5.5)+ Then divide this by N-1 or 9 and take the square root Excal gives: 3.3 as the standard daviation. +(105.5) 110-5.5

Explanation / Answer

Code:

import java.util.*;

//class of Generic Type Number

public class MyMathClass<T extends Number>

{

     //static method total to find the sum of the elements

     //present in the ArrayList and returns a double value

     public static <T extends Number> double total(ArrayList<T> a)

     {

          double total = 0;

          for(T l : a)

          {

              total += l.doubleValue();

          }

          return total;

     }

     //static method sum to find the average of the elements

     //present in the ArrayList and returns a double value

     public static <T extends Number> double average(ArrayList<T> a)

     {

          return total(a)/a.size();

     }

//static method s to find the standard deviation of the //elements

     //present in the ArrayList and returns a double value

     public static <T extends Number> double stdev(ArrayList<T> a)

     {

          double average = average(a);

          double sum = 0.0;

          //loop to calculate the difference between the

          //value and average and find the sum

          //of square value

          for(T l : a) {

              double diff = l.doubleValue() - average;

              sum += diff * diff;

          }

          //return the calculated square root value

          return Math.sqrt(sum / (a.size()-1));

     }

     //main method

     public static void main(String[] args)

     {

          //array list of Double type

          ArrayList<Double> data = new ArrayList<Double>();

          data.add(3.0);

          data.add(5.0);

          data.add(12.6);

          data.add(20.07);

          data.add(15.9);

          double stdevofDouble = stdev(data);

          System.out.println("The elements present in the list are: ");

          for(int i=0;i<data.size();i++)

              System.out.print(data.get(i)+" ");

          System.out.println(" The standard deviation of

ArrayList of Double type is :"+ stdevofDouble);

          System.out.println("-------------------------------------------------------------------------");

        

          //arrayList of Integer type

          ArrayList<Integer> intData = new ArrayList<Integer>();

          intData.add(13);

          intData.add(20);

          intData.add(45);

          intData.add(46);

          intData.add(10);

          double stdevofInteger = stdev(intData);

          System.out.println("The elements present in the list are: ");

          for(int i=0;i<intData.size();i++)

              System.out.print(intData.get(i)+" ");

          System.out.println(" The standard deviation of ArrayList of Integer type is :"+ stdevofInteger);

        

     }

}

-------------------------------------------------------------------------------------------------

Sample Output:

The elements present in the list are:

3.0 5.0 12.6 20.07     15.9

The standard deviation of ArrayList of Double type is :7.21702708876723

-------------------------------------------------------------------------

The elements present in the list are:

13   20   45   46   10

The standard deviation of ArrayList of Integer type is :17.455658108475888