Complete Java program called CalculateAvg according to the f

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

CalculateAverage.java

import java.util.Scanner;

public class CalculateAverage {

   public static void main(String[] args) {
      
       //calling the method which will get the input entered by the user
       String str = getTheNumbers();

       //Calling the method which will calculate the average of the numbers
       double average = calAverage(str);

       //Calling the method which will Displaying the output
       printResults(str, average);

   }

   /* This method will display the average of the number
   * Params:string ,average
   * Return:void
   */
   private static void printResults(String str, double average) {

       //Displaying the average of the numbers
       System.out.println(\"The average of the numbers \" + str + \" is \"+ average);

   }

   /* This method will calculate the average of the numbers
   * Params:string
   * Return:average of type double
   */
   private static double calAverage(String str) {
      
       //parse the string to string array by using the delimeter \" \"
       String arr[] = str.split(\" \");
      
       //Declaring variables
       int sum = 0;
       double average = 0.0;

       //This for loop will calculate the sum of the numbers
       for (int i = 0; i < arr.length; i++) {
           sum += Integer.parseInt(arr[i]);
       }
      
       //calculating the average of the numbers
       average = sum / arr.length;
      
       return average;
   }

   //This method will read the inputs entered by the user
   private static String getTheNumbers() {

       //Declaring variable
       String str = null;

       // Scanner class Object is used to read the inputs entered by the user.
       Scanner in = new Scanner(System.in);

       //Getting the string entered by the user
       System.out.print(\"Enter Number in Single Line :\");
       str = in.nextLine();

       return str;
   }

}

________________________________

Output:Enter Number in Single Line :20 40 60 80 100
The average of the numbers 20 40 60 80 100 is 60.0

________________Thank You

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
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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site