Create a new Java application called "AverageCalculator" (without the quotation marks) that prompts the user with three separate prompts for three numbers (use double values for these), computes the average of those three doubles (use a double for the average), and then prints out explanatory text, the three numbers, and their average, printed with 2 decimal places.For example, if the user gives your program the numbers 30, 100, and 21, your output should look very much like the following: The average of the numbers 30.00, 100.00, and 21.00 is 50.33.

Respuesta :

Answer:

//import the Scanner class

import java.util.Scanner;

public class AverageCalculator {

   public static void main(String[] args) {

       //Create an object of the Scanner class to allow for user's input

       Scanner input = new Scanner(System.in);

       

       //Create first prompt for the user to enter the first number

       System.out.println("Please enter the first number");

       

       //Use the Scanner object input, to receive the first number

       //And store in a double variable called fnum

       double fnum = input.nextDouble();

       

       //Create second prompt for the user to enter the second number

       System.out.println("Please enter the second number");

       

       //Use the Scanner object input, to receive the second number

       //And store in a double variable called snum

       double snum = input.nextDouble();

       

       //Create third prompt for the user to enter the third number

       System.out.println("Please enter the third number");

       

       //Use the Scanner object input, to receive the third number

       //And store in a double variable called tnum

       double tnum = input.nextDouble();

       

       //Declare a variable of type double, called avg, to hold the average of the three numbers

       double avg;

       

       //Now find the average of the three numbers,

       // and store the result of the average in the avg variable declared above

       avg = (fnum + snum + tnum) / 3;

               

       //Print out the result with explanatory text while you convert

       //all numbers to 2 decimal places by using the String.format() method.

       //The String.format() method takes in two arguments - the format descriptor and  

       //the number to be transformed.

       //First argument : In this case, the format descriptor is %.2f

       //Where the % sign signifies that the string (%.2f) is a format specifier.

       //The .2 specifies 2 decimal places. It means, you would use .3 if you wanted the number in 3 decimal places.

       //The f signifies that the number in question is a floating-point number.

       //Second argument : specifies the floating-point number to be transformed.        

       System.out.println("The average of the numbers " + String.format("%.2f", fnum) + ", " + String.format("%.2f", snum) + ", and " + String.format("%.2f", tnum) + " is " + String.format("%.2f", avg));

   

}      //End of main method

   

}   // End of class declaration

Sample output:

---------------------------------------------------------------

>> Please enter the first number

30

>> Please enter the second number

100

>> Please enter the third number

21

>> The average of the numbers 30.00, 100.00, and 21.00 is 50.33

---------------------------------------------------------------

Explanation:

The code above has been written in Java and it contains comments explaining every segment of the code. Please go through the comments carefully for more understanding.

Code without comments:

import java.util.Scanner;

public class AverageCalculator {

   public static void main(String[] args) {  

       Scanner input = new Scanner(System.in);

       System.out.println("Please enter the first number");

       double fnum = input.nextDouble();

       System.out.println("Please enter the second number");

       double snum = input.nextDouble();

     

       System.out.println("Please enter the third number");        

       double tnum = input.nextDouble();

     

       double avg;

       avg = (fnum + snum + tnum) / 3;

               

       System.out.println("The average of the numbers " + String.format("%.2f", fnum) + ", " + String.format("%.2f", snum) + ", and " + String.format("%.2f", tnum) + " is " + String.format("%.2f", avg));

   

}  

   

}