Create using NetBeans a complete Java program called Calcula
Create, using NetBeans, a complete Java program called CalculateAvg according to the following guidelines.
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; and
print the results.
The first method should take no arguments and return a String of numbers separated by spaces.
The second method should take a String as its only argument and return a double (the average).
The third method should take a String and a double as arguments but have no return value.
For example:
If the user input is:
20 40 60 80 100
the program should give as output:
The average of the numbers 20 40 60 80 100 is 60.00.
Solution
CalculateAvg.java
import java.util.Scanner;
public class CalculateAvg {
public static void main(String[] args) {
String numbers = getInput();
double average = calculateAverage(numbers);
printResult(numbers, average);
}
public static String getInput(){
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the numbers: \");
String numbers = scan.nextLine();
return numbers;
}
public static double calculateAverage(String numbers){
String nums[] = numbers.split(\" \");
int sum = 0;
for(int i=0; i<nums.length; i++){
sum = sum + Integer.parseInt(nums[i]);
}
double average = sum/(double)nums.length;
return average;
}
public static void printResult(String numbers, double average){
System.out.println(\"The average of the numbers \"+numbers+\" is \"+average);
}
}
Output:
Enter the numbers:
20 40 60 80 100
The average of the numbers 20 40 60 80 100 is 60.0

