Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1: IN JAVA A) Create two Java classes: StatDriver.java with a main method, and Stats.java without a main method. The goal of the main

Question 1: IN JAVA

A) Create two Java classes: StatDriver.java with a main method, and Stats.java without a main method.

  • The 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.

B) Create an Arraylist of double values: ArrayList data in your main method.

C) 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.

Here are some examples of interacting with the completed program:

java StatDriver mean

1 2 3 4 Mean: 2.50

java StatDriver std

1 2 3 4 StdDev: 1.12

java StatDriver 1 2 3 4 Mean: 2.50

D) 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 ".

E) Add a public static method called stdDev in Stats.java. This method should compute the standard deviation of a given Arraylist of double numbers and return that double number.

  • Your stdDev method should call the mean method created in Part D above.
  • The method should ensure that the Arraylist is not null and that it has more than 0 elements. If either of those conditions exists, return Double.NaN (not a number).
  • Add to the driver program a single line of output following the mean output line. The format of the standard deviation line should be "StdDev: %.2f ".

THANK YOU VERY MUCH!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions