Question: Java This problem is almost exactly like 006 ThreeNumbers problem, except that here you need to use a method to do the calculations. (I will
Java
This problem is almost exactly like 006 ThreeNumbers problem, except that here you need to use a method to do the calculations. (I will post 006 below)
Declare a method named calcSumProdAvg as follows:
private static void calcSumProdAvg(double d1, double d2, double d3){
// write your code for doing the calculations AND printing here. }
In the main method, do all the reading of the lines and doubles. Call this method from the main method to calculate the sum, product, and average and to print them out.
Write a program that reads in three integers and prints out their sum and product and average
Input
The input will be one or more lines of numbers that have been typed in by the user. Each line is to have exactly three real numbers.
Output
The output will have for each line, the numbers that had been read in separated by a single space followed by their sum, product, and average separated by a space.
Each number is to be printed using "%.2e" format (see hint below).
Sample Input
1 2 3
1.1 2.2 3.3
Sample Output
1.00e+00 2.00e+00 3.00e+00 6.00e+00 6.00e+00 2.00e+00
1.10e+00 2.20e+00 3.30e+00 6.60e+00 7.99e+00 2.20e+00
HINT
Here use in.nextDouble() to read a number.
After reading three doubles, use in.nextLine() so that you discard the rest of the "white spaces".
To print two numbers num1 and num2 using %.2e format, I would use System.out.printf("%.2e %.2e ", num1, num2);
The is used to make sure it prints a newline character.
006
import java.util.Scanner;
public class ThreeNumbers {
public static void main(String[] args) { Scanner in = new Scanner(System.in); double average = 3; while(in.hasNextDouble()){ double number1 = in.nextDouble(); double number2 = in.nextDouble(); double number3 = in.nextDouble(); in.nextLine(); double sum = number1 + number2 + number3; double product = number1 * number2 * number3; double avg = sum / average; System.out.printf("%.2e %.2e %.2e %.2e %.2e %.2e ", number1 , number2 , number3 , sum , product , avg); } } }
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
