Question

Language: JAVA(Netbeans) Write a generic class MyMathClass with at type parameter T where T is a...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Given this definition of a generic Linked List node: public class LLNode {     private T...
Given this definition of a generic Linked List node: public class LLNode {     private T data;     private LLNode next;     public LLNode(T data, LLNode next) {           this.data = data;           this.next = next;     }     public void setNext(LLNode newNext){ next = newNext; }     public LLNode getNext(){ return next; }     public T getData() {return data;} } Write the findMinimumNode method body. This method returns the linked list node that contains the minimum value in the...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...
Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation! Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation. Write a new stack implementation, ArrayStack. It should be generic and use...
Modify the following code by JAVA language Write a static method totalDurations that is passed the...
Modify the following code by JAVA language Write a static method totalDurations that is passed the parallel arrays from the main program. It creates a new pair of parallel arrays (phone numbers and durations, again) where each different incoming phone number is stored exactly once, and the duration is the total duration of all the calls from that phone number. It then prints these arrays. Nothing is returned. For example, if the arrays contain 555-1234 (duration 10), 555-4321 (duration 20),...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote...
TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far toString String toString() returns the string "[min,max]" As...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the...
import java.util.ArrayList; /* Rules:         1. Allow Tester to iterate through all nodes using the in-order traversal as the default.             This means, in Tester the following code should work for an instance of this class             called bst that is storing Student objects for the data:                 BinarySearchTree_Lab08<String> bst = new BinarySearchTree_Lab08<String>();                 bst.add("Man");       bst.add("Soda");   bst.add("Flag");                 bst.add("Home");   bst.add("Today");   bst.add("Jack");                ...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT