Question

In: Computer Science

ComputeAverage Write a class called ComputeAverage what it does: asks the user to enter three double...

 

ComputeAverage

Write a class called ComputeAverage what it does: asks the user to enter three double numbers (see examples below), it uses Scanner class to read from standard input each one of the doubles entered by the user. it then prints the average of the three numbers. Suggested steps: 1. prompt the user to enter each of the three doubles by printing. 2. read each of the three doubles using a Scanner. Remember you need to declare a Scanner variable and then use it to call the nextDouble method of Scanner. 3. assign each of the three doubles to its own variable. At this point you have three different variables, each containing one of the doubles entered by the user. 4. compute the average. The average is computed by adding the three numbers and dividing by 3. 6. print the average. Use printf to print the result using only 2 digits after the decimal point. 7. you can run the application by typing: java ComputeAverage

Examples

(user input shown in boldface) % java ComputeAverage Please enter first double: 9 Please enter second double: 9 Please enter third double: 8 The numbers you entered are : 9.0, 9.0, 8.0 The average is: 8.67 % java ComputeAverage Please enter first double: 10 Please enter second double: 12 Please enter third double: 11 The numbers you entered are : 10.0, 12.0, 11.0 The average is: 11.00 %

Solutions

Expert Solution


Code:

import java.util.*;
public class ComputeAverage
{
   public static void main(String args[])           //main method
   {
       Scanner sc=new Scanner(System.in);           //object creation for scanner
       double a,b,c,avg;
       System.out.printf("Please enter first double: ");
       a=sc.nextDouble();                   //taking first double as input
       System.out.printf("Please enter second double: ");
       b=sc.nextDouble();                   //taking second double as input
       System.out.printf("Please enter third double: ");
       c=sc.nextDouble();                   //taking third double as input
       System.out.println("The numbers you entered are: "+a+", "+b+", "+c);       //printing input values
       avg=(a+b+c)/3;                                   //computing average
       System.out.printf("The average is %.2f",avg);                   //printing average upto 2 decimal places
       System.out.println("%");
   }
}


Related Solutions

In a file called Conversions.java, write a program that: Asks the user to enter a double...
In a file called Conversions.java, write a program that: Asks the user to enter a double number. Stores that number into a variable called z. Casts variable z into an integer, and stores the result into a variable called z1. Creates a variable z2, and sets it equal to the integer closest to z. Creates a variable z3, and sets it equal to the floor of z. Creates a variable z4, and sets it equal to the ceiling of z....
Write a class that asks a user to enter three integers. Display them in ascending and...
Write a class that asks a user to enter three integers. Display them in ascending and descending order. Save the file as Sorting.java Again, Don’t forget to create the application/project  SortingTest.java Class that has the main method and an object to use the Sorting class.
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Write a C program that asks the user to enter double values (the values can be...
Write a C program that asks the user to enter double values (the values can be positive, zero, or negative). After entering the first double value, the program asks "Would you like to enter another value?", and if the user enters Y or y, the program continues to get additional values and stores these values into a double array (for up to 100 values — use a symbolic #define constant to specify the 100 size limit). The program will need...
In a file called LengthSum.java, write a program that: Asks the user to enter a string....
In a file called LengthSum.java, write a program that: Asks the user to enter a string. Asks the user to enter a second string. Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please enter a second...
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string. Prints out four different versions of that string: The original version of the string. The upper-case version of the string. The lower-case version of the string. A version where the character at position 0 capitalized, and all other characters in lower case. For example: if the user enters string "gaNDalF12 !! AB3w", your program output should look EXACTLY like this: Please enter a string:...
In a file called DivisibilityTests.java, write a program that: Asks the user to enter an integer....
In a file called DivisibilityTests.java, write a program that: Asks the user to enter an integer. It is OK for your program to crash when the user does not enter a valid integer. If the integer is less than 0, the program prints: "The number is negative." If the integer is divisible by 2 and by 3, the program prints: "The number is even and divisible by 3." If the integer is divisible by 2 but not by 3, the...
In a file called ThreeOperations.java, write a program that: Asks the user to enter two real...
In a file called ThreeOperations.java, write a program that: Asks the user to enter two real numbers (doubles, not integers) called N1 and N2. It is OK if your program crashes when the user does not enter valid double numbers. Computes and displays the following operations between the two: multiplication, division, exponentiation. For the results of these two operations, only two decimal digits should be displayed. For example: if the user enters numbers 11 and 7, your program output should...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
Write C++ void function called averageTemperature() that asks the user to enter integers with a sentinel...
Write C++ void function called averageTemperature() that asks the user to enter integers with a sentinel value of -999 to stop entering numbers. Then average the numbers and print the average. You will need an int acculumator for the temperatures and an int acculumator for the number of temperatures entered. Use the accumulators to calculate the average which will need to be a decimal number. Call the function from the main() function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT