Question

In: Computer Science

Using jGRASP, write a Java program named LastnameFirstname11.java, using your last name and your first name,...

Using jGRASP, write a Java program named LastnameFirstname11.java, using your last name and your first name, that does the following:

  • Defines a method called generateNums. This method will create an array of size x, fill it with random numbers between y and z, then return the array. When you call the method, you must specify the size of the array (x), the beginning of your random number range (y), and the ending of your random number range (z).
  • Defines a method called avgNums. You will be able to pass a double array to this method. This method will add up all the numbers in the array. Then it will divide that sum by the size of the array to find the average of all the numbers. Return the average.
  • Asks the user for input to determine the array size and what range of random numbers to generate.
  • Calls the generateNums method 5 times to create arrays based on the user's input.
    • Store each of the returned arrays into a variable.
  • Calls the avgNums method 5 times.
    • Store each of the returned arrays into a variable.
  • Prints the results, displaying 4 digits after the decimal.

Solutions

Expert Solution

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


Related Solutions

Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name, that does the following: Create two arrays that will hold related information. You can choose any information to store, but here are some examples: an array that holds a person's name and an array that hold's their phone number an array that holds a pet's name and an array that holds what type of animal that pet is an array that holds a student's...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name, that does the following: Declare an array reference variable called myFavoriteSnacks for an array of String type. Create the array so that it is able to hold 10 elements - no more, no less. Fill the array by having each array element contain a string stating one of your favorite foods/snacks. Note: Only write the name of the snack, NO numbers (i.e. Do not...
jgrasp environment, java write a complete program that prompts the user to enter their first name,...
jgrasp environment, java write a complete program that prompts the user to enter their first name, middle name, and last name (separately). print out thier name and initials, exactly as shown: your name is: John Paul Chavez your initials are: J. P. C. use string method chartAt() to extract the first (zero-th) character from a name(the name is a string type): username.charAt(0). thank you.
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other.
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other
java programming write a program with arrays to ask the first name, last name, middle initial,...
java programming write a program with arrays to ask the first name, last name, middle initial, IDnumber and 3 test scores of 10 students. calculate the average of the 3 test scores. show the highest class average and the lowest class average. also show the average of the whole class. please use basic codes and arrays with loops the out put should look like this: sample output first name middle initial last name    ID    test score1 test score2...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
Write a class named Person with data attributes for a person’s first name, last name, and...
Write a class named Person with data attributes for a person’s first name, last name, and telephone number. Next, write a class named Customer that is a subclass of the Person class. The Customer class should have a data attribute for a customer number and a Boolean data attribute indicating whether the customer wishes to be on a calling list. Demonstrate an instance of the Customer class in a simple program. Using python
Write a program named filemaker.py in python that will be used to store the first name...
Write a program named filemaker.py in python that will be used to store the first name and age of some friends in a text file named friends.txt. The program must use a while loop that prompts the user to enter the first name and age of each friend. Each of these entries should be written to its own line in the text file (2 lines of data per friend). The while loop should repeat until the user presses Enter (Return...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT