Question
Create two Java classes StatDriver.java with a main method and Stats.java without a main method, goal of the main method is to read a series
-
Create two Java classes StatDriver.java with a main method and Stats.java without a main method, goal of the main method is to read a series of numbers from System.in, create an Arraylist containing those numbers, call the mean and standard deviation functions of the Stats class, and then report the results.
-
While executing your program, user should be able to provide a string on the command line indicating whether they want to calculate the mean ("mean") or the standard deviation ("std"). If no argument is provided, the mean should be calculated.
-
While entering the numbers from the terminal, as program input, the user should be able to indicate that no more numbers will be entered by pressing CTRL-d or any non-number character (ex.letters A-Z) in the terminal.
-
To start coding, first create an Arraylist of double values: ArrayList
data in your main method. -
Now, use a loop to read the input values from the keyboard (as doubles) and store them into the Arraylist. No prompt should be given before reading the values. The loop should terminate when user indicates that no more numbers will be entered by pressing CTRL-d in the terminal.
- The following lines of code will allow you to read the numbers from the terminal and will store it in an Arraylist named data, till user presses CTRL-d in the terminal:
Scanner input = new Scanner(System.in);
while (input.hasNextDouble()) { data.add(input.nextDouble()); }
-
Note: The condition of the while loop works for anything other than numbers. User can either press ctrl-d or any non-number (ex.letters A-Z) in the keyboard to stop taking input.
-
Once the Arraylist is built, if the user wanted to compute mean, you will call the appropriate method (either mean or stdDev) of Stats class and pass this Arraylist as the argument.
-
In the Stats.java file, create a public static method called mean that will take an Arraylist of double values as its parameter, and then calculate and return the mean of those numbers (which is of double datatype).
-
The method should ensure that the Arraylist is not null and that it has more than 0 elements. If either of these conditions exists, the method should return Double.NaN (not a number).
-
Test your mean method from the StatDriver.java program. The main method should output the return value as a single line, using the format "Mean: %.2f ".
THANK YOU!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started