Complete the definitions of three methods that are called in the main method. Co
ID: 3708353 • Letter: C
Question
Complete the definitions of three methods that are called in the main method.
Complete the code in the TODO steps within
/*
* The program is to read a certain number of real values,
* get the average of these items, and find the number
* of the items that is greater than the average.
*
* Define the three methods that are called in the main method.
* The headers of the methods are given below the main method.
* You need to complete the method body.
*
* You can refer to the program at Section 7.3 (Page 253) in the textbook.
* The program here does the same thing, except that this program uses
* methods instead of doing everything in the main method.
*/
public class AnalyzeNumbers {
public static void main(String[] args) {
//PLEASE DO NOT CHANGE ANYTHING IN THE MAIN METHOD!!!!!!!!!!!!!!
//create an array with user specified dimention and values
double[] numbers = initArrayFromUserInputs();
//get the average of the values in numbers
double average = getAverage(numbers);
// get the numbers of elements above average
int count1 = countAboveValue(numbers, average);
int count2 = numbers.length - count1; //The numbers of elements at or below average
System.out.println("Average is " + average);
System.out.println("Number of elements above the average is "
+ count1);
System.out.println("Number of elements at or below the average is "
+ count2);
} //end of main
/*
* Method nitArrayFromUserInputs:
* The method will prompt user to enter the number of real values
* the user wants to analyze, read the real values, store them in
* an array, and finally return the array.
*/
public static double[] initArrayFromUserInputs() {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the number of values to analyze: ");
int numValues = input.nextInt();
//create an array with capacity numValues
double[] values = new double[numValues];
System.out.print("Enter the numbers: ");
// TODO1: Write a for loop below to read numValues double values,
// store them in array values.
return values;
} //end of nitArrayFromUserInputs
/*
* Method getAverage:
* Given a double array, return the average of the values in the array.
*/
public static double getAverage(double[] nums) {
// TODO2: Change the default body below to a real implementation.
return 0.0;
} //end of getAverage
/*
* Method countAboveValue:
* Given a double array and a double value, return count of numbers in
* the array that is above the given value.
*/
public static int countAboveValue(double[] nums, double value) {
// TODO3: Change the default body below to a real implementation.
return 0;
} //end of countAboveValue
}
Explanation / Answer
AnalyzeNumbers.java
public class AnalyzeNumbers {
public static void main(String[] args) {
//PLEASE DO NOT CHANGE ANYTHING IN THE MAIN METHOD!!!!!!!!!!!!!!
//create an array with user specified dimention and values
double[] numbers = initArrayFromUserInputs();
//get the average of the values in numbers
double average = getAverage(numbers);
// get the numbers of elements above average
int count1 = countAboveValue(numbers, average);
int count2 = numbers.length - count1; //The numbers of elements at or below average
System.out.println("Average is " + average);
System.out.println("Number of elements above the average is "
+ count1);
System.out.println("Number of elements at or below the average is "
+ count2);
} //end of main
/*
* Method nitArrayFromUserInputs:
* The method will prompt user to enter the number of real values
* the user wants to analyze, read the real values, store them in
* an array, and finally return the array.
*/
public static double[] initArrayFromUserInputs() {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter the number of values to analyze: ");
int numValues = input.nextInt();
//create an array with capacity numValues
double[] values = new double[numValues];
System.out.print("Enter the numbers: ");
// TODO1: Write a for loop below to read numValues double values,
// store them in array values.
for(int i=0;i<numValues;i++){
values[i]=input.nextDouble();
}
return values;
} //end of nitArrayFromUserInputs
/*
* Method getAverage:
* Given a double array, return the average of the values in the array.
*/
public static double getAverage(double[] nums) {
// TODO2: Change the default body below to a real implementation.
double sum = 0;
for(int i=0;i<nums.length;i++) {
sum+=nums[i];
}
return sum/nums.length;
} //end of getAverage
/*
* Method countAboveValue:
* Given a double array and a double value, return count of numbers in
* the array that is above the given value.
*/
public static int countAboveValue(double[] nums, double value) {
// TODO3: Change the default body below to a real implementation.
int count = 0;
for(int i=0;i<nums.length;i++) {
if(nums[i]>value){
count++;
}
}
return count;
} //end of countAboveValue
}
Output:
Enter the number of values to analyze: 10
Enter the numbers: 4
5
3
2
6
7
8
910
1
10
Average is 95.6
Number of elements above the average is 1
Number of elements at or below the average is 9