Need Help please Write a Java program using the following methods: A method to f
ID: 3762115 • Letter: N
Question
Need Help please
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.
Sample output:
Do you want to start(Y/N): y
The array elements are:
56 90 14 85 39 34
83 81 79 7 63 72
65 56 52 30 91 34
8 23 91 81 63 100
90 22 26 37 13 90
The difference between the max value 100 and the min value 7 = 93
Do you want to continue(Y/N): y
The array elements are:
29 37 46 94 2 13
7 79 91 41 46 23
61 17 55 75 22 30
28 71 71 2 63 52
8 7 83 64 27 24
The difference between the max value 94 and the min value 2 = 92
Do you want to continue(Y/N): y
The array elements are:
55 48 60 23 43 35
25 54 74 18 78 16
44 30 70 25 6 24
93 42 39 91 1 84
76 40 35 1 17 79
The difference between the max value 93 and the min value 1 = 92
Do you want to continue(Y/N): n
Explanation / Answer
Program code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
public class array {
int maxSize=30;
int a[]=new int[maxSize],i;
Random r=new Random();
void fillArray()
{
for(int i=0;i<maxSize;i++)
{
a[i]=r.nextInt(100);
}
}
void printArray()
{
for( i=0;i<maxSize;i++)
{
if(i%6==0)
{
System.out.println();
}
System.out.print(a[i]+" ");
}
}
int max()
{
int max1=a[0];
for( i=0;i<maxSize;i++)
{
if(max1<a[i])
max1=a[i];
}
return max1;
}
int min()
{
int min=a[0];
for( i=0;i<maxSize;i++)
{
if(min>a[i])
min=a[i];
}
return min;
}
public static void main(String args[]) throws IOException
{
char in1;
System.out.print("Do you want to start [Y/N] ");
do
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
in1=(char)in.read();
if(in1=='y'||in1=='Y')
{
array aop=new array();
aop.fillArray();
aop.printArray();
int a=aop.max();
int b=aop.min();
System.out.println(" The difference between maximum value"+a+" and the min value "+b+"="+(a-b));
System.out.print("Do you want to Continue [Y/N] ");
}
else
System.exit(1);
} while(in1=='y'||in1=='Y');
}
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.