Question: Here is the question: Design and implement a class called RandomArray, which has an integer array. The constructor receives the size of the array to
Here is the question:
-
Design and implement a class called RandomArray, which has an integer array. The constructor receives the size of the array to be allocated, then populates the array with random numbers from the range 0 through the size of the array. Methods are required that return the minimum value, maximum value, average value, and a String representation of the array values. Document your design with a UML Class diagram. Create a separate driver class that prompts the user for the sample size, then instantiates a RandomArray object of that size and outputs its contents and the minimum, maximum, and average values.
Hint: Obviously the minimum and maximum values will be integers, but the average will need to allow decimals.
Testing: Include the output for several different test runs of various sizes, that shows the array contents with its minimum, maximum, and average values.
Here is my code:
/* Design and implement a class called RandomArray, which has an integer array. The constructor receives the size of the array to be allocated, then populates the array with random numbers from the range 0 through the size of the array. Methods are required that return the minimum value, maximum value, average value, and a String representation of the array values. Document your design with a UML Class diagram. Create a separate driver class that prompts the user for the sample size, then instantiates a RandomArray object of that size and outputs its contents and the minimum, maximum, and average values.
Hint: Obviously the minimum and maximum values will be integers, but the average will need to allow decimals.
Testing: Include the output for several different test runs of various sizes, that shows the array contents with its minimum, maximum, and average values. */
import java.util.Scanner;
public class Assignment5Q1 { public static void main(String args[]) { int max; int min; int size; double avg; Scanner input = new Scanner(System.in);
System.out.println("Enter the size of the array"); size = input.nextInt(); // Accepting the length from user
RandomArray randomArray = new RandomArray(size); // Object of the class max = randomArray.maximum(); // Calling the methods min = randomArray.minimum(); avg = randomArray.average();
// Displaying the results System.out.println("Elements of the array"); System.out.println(randomArray); System.out.println("The maximum number is : " + max); System.out.println("The minimum number is : " + min); System.out.println("The average is : " + avg); } } class RandomArray { private int[] randNumbers;
public RandomArray(int size) // Constructor to initialise the array { randNumbers = new int[size]; for(int i = 0; i < randNumbers.length; i++) randNumbers[i] = (int)(Math.random() * (randNumbers.length + 1)); }
public int maximum() // Caclulating the maximum number { int max = randNumbers[0]; for(int i = 1; i < randNumbers.length; i++) if(randNumbers[i] > max) max = randNumbers[i];
return max; // Returning the max number }
public int minimum() // Calculating the minimum number { int min = randNumbers[0]; for(int i = 1; i < randNumbers.length; i++) if(randNumbers[i] < min) min = randNumbers[i];
return min; // Returning the min number }
public double average() // Calculating the average of the numbers { int sum = 0; double avg;
for(int i = 0; i < randNumbers.length; i++) sum += randNumbers[i];
avg = (double) sum / randNumbers.length; return avg; // Returning the average }
@Override public String toString() // String representation of the array elements { String result = "["; // Appending all elements of the array to the result string except the last one for(int i = 0; i < randNumbers.length - 1; i++) result += randNumbers[i] + ", ";
// Appending the last element of the array to the result string if the array is not empty // otherwise appending a closing bracket if(randNumbers.length != 0) result += randNumbers[randNumbers.length - 1] + "]"; else result += "]";
return result; // Returning the string representation of the array elements } }
Please fix any issues in my code or comment if it's all correct. Thank you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
