COM S 207 ThreeNumbersM This problem is almost exactly like

COM S 207

ThreeNumbersM

This problem is almost exactly like 006 ThreeNumbers problem, except that here you need to use a method to do the calculations.

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 Page 2 of 2 pages 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. The printf method can be found in your textbook – section 2.5.5 Formatted Output

Solution

ThreeNumbersM.java

import java.util.Scanner;


public class ThreeNumbersM {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print(\"Enter the first number: \");
       double d1 = scan.nextDouble();
       System.out.print(\"Enter the second number: \");
       double d2 = scan.nextDouble();
       System.out.print(\"Enter the third number: \");
       double d3 = scan.nextDouble();
       calcSumProdAvg(d1,d2,d3);

   }
   private static void calcSumProdAvg(double d1, double d2, double d3){
       double sum = d1+d2+d3;
       double average = sum/3;
       double product = d1 * d2 * d3;
       System.out.printf(\"Sum of %.2e %.2e %.2e is %.2e\ \", d1, d2, d3, sum);
       System.out.printf(\"Average of %.2e %.2e %.2e is %.2e\ \", d1, d2, d3, average);
       System.out.printf(\"Product of %.2e %.2e %.2e is %.2e\ \", d1, d2, d3, product);
   }

}

Output:

Enter the first number: 1.1
Enter the second number: 2.2
Enter the third number: 3.3
Sum of 1.10e+00 2.20e+00 3.30e+00 is 6.60e+00
Average of 1.10e+00 2.20e+00 3.30e+00 is 2.20e+00
Product of 1.10e+00 2.20e+00 3.30e+00 is 7.99e+00

COM S 207 ThreeNumbersM This problem is almost exactly like 006 ThreeNumbers problem, except that here you need to use a method to do the calculations. Declare
COM S 207 ThreeNumbersM This problem is almost exactly like 006 ThreeNumbers problem, except that here you need to use a method to do the calculations. Declare

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site