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
-
Your Stats class should be set up as shown in the UML on the next page. It should have:
-
Private data members to store the array and each statistical value, as named in the UML diagram
-
A no-args constructor
-
A constructor that just takes in an array of doubles
-
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.
-
A toString() that returns a nicely formatted (4 Decimal places) String showing all 4 statistics, each with a label! (See example on next page.)
-
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.
-
-
Your ThousandRandoms class should:
-
Have a main method
-
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().)
-
Create a Stats object and pass in the array through the constructor
-
Call the calculateStatistics() method on the Stats object so that statistics are determined.
-
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:
-
Use a constant to represent the value 1000 for the array.
-
Use the Random class nextDouble() method to generate all the random doubles.
-
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.
-
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
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