Exercise#3: Write a ch program that performs the following: Declare a two arrays called arrayA and arrayB that holds integer and the size of each array is given by the user. b. The main method calls a method called fillArray(...) of type void to fill an array with a set of generated random integers between low and high (both inclusive). This method should be called twice to fill arrayA and then arrayB. (the values of High and low are read in the main method. ​

Respuesta :

Answer:

The code for this problem is completed here. Comments are added, go and learn how things work.

Explanation:

// Arrays.java

import java.util.Random;

import java.util.Scanner;

public class Arrays {

     // method to fill an array with random numbers between low and high

     static void fillArray(int array[], int low, int high) {

           Random random = new Random();

           for (int i = 0; i < array.length; i++) {

                 // generating a value between low and high inclusive, adding to

                 // array

                 array[i] = random.nextInt(high - low + 1) + low;

           }

     }

     // method to print elements in an array

     static void printArray(int array[]) {

           for (int i = 0; i < array.length; i++) {

                 // displaying elements separated by tab space

                 System.out.print(array[i] + "\t");

                 // printing a line break after every 5th element, or if this is the

                 // last element.

                 if ((i + 1) % 5 == 0 || i == array.length - 1) {

                       System.out.println();

                 }

           }

     }

     // method to count the number of elements in array greater than value

     static int count(int array[], int value) {

           // initializing count to 0

           int c = 0;

           for (int i = 0; i < array.length; i++) {

                 if (array[i] > value) {

                       // incrementing c

                       c++;

                 }

��          }

           return c;

     }

     // returns true if two arrays are same, else false

     static boolean isSame(int array1[], int array2[]) {

           if (array1.length != array2.length) {

                 // lengths mismatch

                 return false;

           }

           // checking if elements are same in same order

           for (int i = 0; i < array1.length; i++) {

                 if (array1[i] != array2[i]) {

                       // mismatch found

                       return false;

                 }

           }

           // same

           return true;

     }

     // returns the average of elements in an array

     static double findAverage(int array[]) {

           double sum = 0;

           // summing values

           for (int i = 0; i < array.length; i++) {

                 sum += array[i];

           }

           // finding average

           double avg = (double) sum / array.length;

           return avg;

     }

     // method to count the number of values greater than average of an array

     static int aboveAverage(int array[]) {

           // finding average of passed array using findAverage method

           double avg = findAverage(array);

           // finding number of elements in array>avg using count method

           int c = count(array, (int) avg);

           return c;

     }

     public static void main(String[] args) {

           // setting up a Scanner

           Scanner scanner = new Scanner(System.in);

           // prompting and reading size of arrayA

           System.out.print("Enter size of arrayA: ");

           int n1 = scanner.nextInt();

           // initializing arrayA

           int arrayA[] = new int[n1];

           // doing the same for arrayB

           System.out.print("Enter size of arrayB: ");

           int n2 = scanner.nextInt();

           int arrayB[] = new int[n2];

           // asking and reading low and high values

           System.out.print("Enter low and high values for random numbers: ");

           int low = scanner.nextInt();

           int high = scanner.nextInt();

           // filling both arrays

           fillArray(arrayA, low, high);

           fillArray(arrayB, low, high);

           // printing both arrays

           System.out.println("arrayA:");

           printArray(arrayA);

           System.out.println("arrayB:");

           printArray(arrayB);

           // reading an integer

           System.out.print("Enter an integer value: ");

           int value = scanner.nextInt();

           // displaying the number of elements in each array greater than above

           // value

           System.out.println("The number of elements greater than " + value

                       + " in arrayA: " + count(arrayA, value));

           System.out.println("The number of elements greater than " + value

                       + " in arrayB: " + count(arrayB, value));

           System.out.println("Both arrays are same: " + isSame(arrayA, arrayB));

           // finding and displaying average of arrayA

           double avgA = findAverage(arrayA);

           System.out.println("Average of all values in arrayA: " + avgA);

           // finding and displaying number of elements in arrayB greater than the

           // average of arrayB

           System.out.println("The number of elements greater than "

                       + "the average of arrayB" + " in arrayB: "

                       + aboveAverage(arrayB));

     }

}