In: Computer Science
Using jGRASP, write a Java program named LastnameFirstname11.java, using your last name and your first name, that does the following:
I have created a class whose name is LastnameFirstname11 and If you want to change the name of the main class to your lastname and firstname, you can do if it is important
The code to generate the random elements in the array and to find the average of the elements of the array is as follow:
import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
class LastnameFirstname11 {
private static Random random = new Random();
// Generate a double array with given size, minimun and maximum value
public static double[] generateNums(int x, int min, int max) {
double[] array = new double[x];
for(int i = 0; i< x; i++) {
array[i] = (Math.random() * (max - min + 1) + min);
}
return array;
}
// Generate the average of the elements in the given array
public static double avgNums(double[] array) {
double sum = 0;
for(int i =0; i<array.length; i++) {
sum += array[i];
}
return sum/array.length;
}
// main method which handle the execution of the above functions
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
double[] averageValues = new double[5];
double[][] doubleArrays = new double[5][];
int size, min, max;
System.out.println("Enter the information for 5 times \n");
for(int i =0; i<5; i++ ) {
System.out.println("Enter the size of array " + (i + 1));
size = scan.nextInt();
System.out.println("Enter the Minimum value of array " + (i + 1));
min = scan.nextInt();
System.out.println("Enter the Maximum value of array " + (i + 1));
max = scan.nextInt();
doubleArrays[i] = generateNums(size, min, max);
averageValues[i] = avgNums(doubleArrays[i]);
}
for(int i =0; i < doubleArrays.length; i++) {
System.out.println("\nElements of array " + (i + 1));
for(int j = 0; j < doubleArrays[i].length; j++) {
System.out.printf("%.4f ", doubleArrays[i][j]);
}
System.out.printf("\nThe Average value of the above Array is %.4f", averageValues[i], "\n");
System.out.println("\n---------------------------------------------------\n");
}
}
}
I am even attaching the output images
If you have any doubts you can add a comment in the comments section. Thank you