Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program using the following methods: A method to fill the array wit

ID: 3759598 • Letter: W

Question

Write a Java program using the following methods:

A method to fill the array with random numbers: the elements of the array should be random integers between 0 and 100 (declared as class constants).

A method to print the elements of the array in a table like format (6 elements/line - declared as a class constant)

A method to find the maximum element in the array

A method to find the minimum element in the array

Have the size of the array declared as a class constant (30). The methods should take as input parameters the array and its size. The method to print should add one more parameter for the number of elements on each line. Write a driver program to call these methods and display the difference between the maximum and the minimum element in the array.

Explanation / Answer

Answer :

import java.util.*;
class RR
{
   static int max;
   public static void main(String args[])
   {
  
       int randomNumbers[] = new int[30];
      
       Random rand = new Random();
       for (int i = 0; i < randomNumbers.length; i++)
       {
            int n = rand.nextInt(100);
           randomNumbers[i] = n;
           System.out.println("Position " + i + " : " + randomNumbers[i]);
       }
      
       int smallest = randomNumbers[0];
int largest = randomNumbers[0];
   for(int i=1; i< randomNumbers.length; i++)
{
if(randomNumbers[i] > largest)
largest =randomNumbers[i];
else if (randomNumbers[i] < smallest)
smallest = randomNumbers[i];

}
System.out.println("Largest Number is : " + largest);
System.out.println("Smallest Number is : " + smallest);
       int difference=largest-smallest;
  
   System.out.println("The difference between the maximum and the minimum element

in the array is : " + difference);  
   }
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote