Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

JAVA Your Stats class should be set up as shown in the UML on the next page. It should have: Private data members to store

JAVA

  1. Your Stats class should be set up as shown in the UML on the next page. It should have:

    1. Private data members to store the array and each statistical value, as named in the UML diagram

    2. A no-args constructor

    3. A constructor that just takes in an array of doubles

    4. A public method that calculates the statistics and sets the answers to the 4 other data members (the number of elements, minimum value, maximum value, average value). Please determine the minimum, maximum, and average using typical math operations and Math methods. Do not search out Array methods to do this for you. Also, find and set all three values within just one iteration (loop) through the array.

    5. A toString() that returns a nicely formatted (4 Decimal places) String showing all 4 statistics, each with a label! (See example on next page.)

    6. Do not worry about setters or getters. All values (except the array) are generated from within the Stats class and the array gets set via the 2nd constructor.

  2. Your ThousandRandoms class should:

    1. Have a main method

    2. Use the Random class to generate an array containing 1000 random doubles. Do not specify a range. (You may look up the Random class as a refresher. Use nextDouble(), not nextInt().)

    3. Create a Stats object and pass in the array through the constructor

    4. Call the calculateStatistics() method on the Stats object so that statistics are determined.

    5. Call the toString() method and print the results.

Stats

-values: double[ ]

-numberOfElements: double

-minimumValue: double

-maximumValue: double

-averageValue: double

+Stats()

+Stats(double[ ])

+calculateStatistics(): void

+toString(): String

The only output to the screen at check-off should be a block of statistics. Below is an example of what I expect to see: Additional specifications are on the next page.

Number of values: 1000

Minimum value: 0.0101

Maximum value: 0.9490

Average value: 0.4921

Additional Specifications:

  1. Use a constant to represent the value 1000 for the array.

  2. Use the Random class nextDouble() method to generate all the random doubles.

  3. The calculateStatistics() method should be efficient and should only iterate through the array once! Try to find all three pieces of information while looping through the array values one time.

  4. The Stats class toString() method should return a formatted String to 4 decimal places. Use the String class static method format for this purpose. Here is an example of a toString() method that uses the String class format() method to beautify three doubles. (Its like using System.out.printf() but instead of printing the formatted values, we are just applying the formatting to a String. Remember, a toString does not contain a print statement but we might still want to format the values.)

The String.format() method is a static method that can be thought of as a String utility.

public String toString() {

return String.format("%8s%5.3f %8s%5.3f %8s%5.3f",

"first:", values[0],

"Second:", values[1],

"third:", values[2]);

}

Find details about String formatting in our book, pages 145 148. Notice that the book describes the System.out.printf method that is used to display formatted output on the console. It is good to know that the String.format() method uses the same formatting string details.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

More Books

Students explore these related Databases questions