Write a JAVA program that creates and initialize an array of 50 random integers
ID: 3707023 • Letter: W
Question
Write a JAVA program that creates and initialize an array of 50 random integers between 1 and 1000 and make the following methods and display the results:
?getHighest() – this method will return thehighest elementin the array.
?getLowest() – this method will return theindex of the lowest element in the array.
Notice how this is different than getHighest().
?getAverage() – this method will return the Average of all elements in the array.
?getAboveAverage() – this method returns the number of elements in the array above the average value.
Hint: don’t forget that you can call methods from within other methods.
?getBelowAverage() – this method returns the number of elements in the array below the average.
Note: when making these methods, don’t forget to think about what the return type should be, and what to include for parameters.
Include proper input and output to test each of these methods back in the main method.
Explanation / Answer
import java.util.Random;
public class Driver {
public static void main(String[] args){
Random r = new Random();
int Low = 1;
int High = 1000;
int numbers[] = new int[50];
for(int i = 0; i < 50; i++)
numbers[i] = r.nextInt(High-Low) + Low;
System.out.println("The highest element in the array is " + getHighest(numbers));
System.out.println("The highest element in the array is " + getLowest(numbers));
System.out.println("The average of the array is " + getAverage(numbers));
System.out.println("The number of elements in the array above the average value is " + getAboveAverage(numbers));
System.out.println("The number of elements in the array below the average value is " + getBelowAverage(numbers));
}
private static int getBelowAverage(int[] numbers) {
double avg = getAverage(numbers);
int count = 0;
for(int i = 0; i < 50; i++)
if(numbers[i] < avg)
count++;
return count;
}
private static int getAboveAverage(int[] numbers) {
double avg = getAverage(numbers);
int count = 0;
for(int i = 0; i < 50; i++)
if(numbers[i] > avg)
count++;
return count;
}
private static double getAverage(int[] numbers) {
double sum = 0;
for(int i = 0; i < 50; i++)
sum += numbers[i];
return sum / numbers.length;
}
private static int getLowest(int[] numbers) {
int number = numbers[0];
for(int i = 1; i < 50; i++)
if(numbers[i] < number)
number = numbers[i];
return number;
}
private static int getHighest(int[] numbers) {
int number = numbers[0];
for(int i = 1; i < 50; i++)
if(numbers[i] > number)
number = numbers[i];
return number;
}
}
**Comment for any further queries. Upvote if the answer is satisfactory
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.