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)...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
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...
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...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
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...
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...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
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...
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