This Solution needs to be one java file not multiples This S
This Solution needs to be one java file not multiples.
This Solution also needs proper java formatting.
This Solution MUST USE ARRAYS
Create, using NetBeans, a complete Java program called CalcAvg according to the following guidelines. (This program requires the use of arrays.)
The program prompts the user for five to ten numbers all on one line, separated by spaces, calculates the average of those numbers, and displays the numbers and their average to the user.
The program uses methods to:
get the numbers entered by the user
calculate the average of the numbers entered by the user
print the results.
The first method should take no arguments and return an array of doubles that the user entered.
The second method should take an array of doubles (the return value of the first method above) as its only argument and return a double (the average).
The third method should take an array of doubles and a (single) double value as arguments but have no return value.
For example:
If the user input is:
40 60 20 80 100
Then the program should give as output:
The average of the numbers 40.00, 60.00, 20.00, 80.00, and 100.00 is 60.00.
Solution
CalculateAvg.java
import java.util.Arrays;
import java.util.Scanner;
public class CalculateAvg {
public static void main(String[] args) {
double numbers[] = getInput();
double average = calculateAverage(numbers);
printResult(numbers, average);
}
public static double[] getInput(){
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the numbers: \");
String numbers = scan.nextLine();
String nums[] = numbers.split(\" \");
double d[] = new double[nums.length];
for(int i=0; i<d.length; i++){
d[i] = Double.parseDouble(nums[i]);
}
return d;
}
public static double calculateAverage(double nums[]){
double sum = 0;
for(int i=0; i<nums.length; i++){
sum = sum + nums[i];
}
double average = sum/(double)nums.length;
return average;
}
public static void printResult(double numbers[], double average){
System.out.println(\"The average of the numbers \"+Arrays.toString(numbers)+\" is \"+average);
}
}
Output:
Enter the numbers:
40 60 20 80 100
The average of the numbers [40.0, 60.0, 20.0, 80.0, 100.0] is 60.0

