Question

specs for ComputeAverage ComputeAverage Write a class called ComputeAverage what it does: asks the user to...

specs for ComputeAverage

 

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 %

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ComputeAverage.java

import java.util.Scanner;

public class ComputeAverage

{

public static void main(String args[])

{

// creating Scanner class object to get input from user

Scanner sc=new Scanner(System.in);

System.out.print("Please enter first double: ");

double first=sc.nextDouble(); // getting first number

System.out.print("Please enter second double: ");

double second=sc.nextDouble(); // getting second number

System.out.print("Please enter third double: ");

double third=sc.nextDouble(); // getting third number

System.out.printf("\nThe numbers you entered are: %.1f,%.1f,%.1f",first,second,third);

double sum=first+second+third; // calculate sum

double average=sum/3; // calculate average

// display the average

System.out.printf("\nThe average is: %.2f",average);

System.out.print("%");

sc.close(); // closing Scanner object

}

}

Output

C:Users VAKSHAY\Desktop javac ComputeAverage.java 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%

Add a comment
Know the answer?
Add Answer to:
specs for ComputeAverage ComputeAverage Write a class called ComputeAverage what it does: asks the user to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A class Scanner in Java can be used to get user input and its methods can...

    A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed: Scanner input = new Scanner(System.in); One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally the...

  • (a) A class Scanner in Java can be used to get user input and its methods...

    (a) A class Scanner in Java can be used to get user input and its methods can be used after the following statement is executed: Scanner input = new Scanner(System.in); One of its methods nextDouble() returns the next double value from the keyboard. Now you are requested to write a method enterHeight() which displays the message "Input height: " and uses the method input.nextDouble() to get the user input. The process should be repeated until the input is non-negative. Finally...

  • Download PartiallyFilledArray.java. Though we studied this in class, you should still take several minutes examining the...

    Download PartiallyFilledArray.java. Though we studied this in class, you should still take several minutes examining the code so that you understand the methods of the class. /** * This is a solution to the lab, "partially filled array". * * * */ import java.util.Scanner; public class CalculateAverage { public static void main(String[] args){ PartiallyFilledArray data = new PartiallyFilledArray(); getInput(data); printResults(data); }    private static void getInput(PartiallyFilledArray data) { Scanner kbd = new Scanner(System.in); System.out.print("Enter a number (negative will end input):");...

  • In Java, write a program that asks the user for the name of a file. The...

    In Java, write a program that asks the user for the name of a file. The program should read all the numbers from the given file and display the total and average of all numbers in the following format (three decimal digits): Total: nnnnn.nnn Average: nnnnn.nnn Class name: FileTotalAndAverage

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Write a Java program that: • Asks the user to enter the number of integers that...

    Write a Java program that: • Asks the user to enter the number of integers that he need to enter; • Asked him to enter integer by integer; • Print The number of Odd numbers entered and the number of Even numbers entered. Important notes: 1. You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code. It must be editable. 2. Take a screen shot for your...

  • Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input...

    Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total. Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it...

  • Write a C program that asks the user to enter three numbers (integres). A menu will...

    Write a C program that asks the user to enter three numbers (integres). A menu will be displayed to let the user choose one of the three options: print the two highest numbers, product of the three numbers or the division of the second by the by the third if the third is not zero. See the sample runs. Required: Your code must use -One function to get the numbers from the user One function for each of the three...

  • Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a...

    Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a number of properties about an array of doubles. There should be separate static methods to calculate the min, the max, the mean, the median, the standard deviation (make sure to make use of your mean method to implement this), and the mode. For the mode, you can assume that the data set is not multimodal. This class should not have a main method. Write...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT