Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program that reads two numbers from the console. Based on the examp

ID: 3682526 • Letter: W

Question

Write a Java program that reads two numbers from the console. Based on the example in the textbook, write five methods (min, max, sum, difference, and product) to calculate and return the appropriate results. For the difference method, use the absolute value method and the round method from the Math class to refine your results to the nearest whole positive integer.

Here are two sample runs:

Enter two numbers: 2 3

The minimum of 2.0 and 3.0 is 2.0

The maximum of 2.0 and 3.0 is 3.0

The sum of 2.0 and 3.0 is 5.0

The difference between 2.0 and 3.0, rounded to the nearest whole integer, is 1

The product of 2.0 and 3.0 is 6.0

Enter two numbers: 11.75 1.5

The minimum of 11.75 and 1.5 is 1.5

The maximum of 11.75 and 1.5 is 11.75

The sum of 11.75 and 1.5 is 13.25

The difference between 11.75 and 1.5, rounded to the nearest whole integer, is 10

The product of 11.75 and 1.5 is 17.625

Explanation / Answer

SOlution:

package com.nancy.chegg.qanda;

import java.util.Scanner;

public class Reader {

   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter two numbers: ");
       double num1 = scan.nextDouble();
       double num2 = scan.nextDouble();

       System.out.println("The minimum of " + num1 + " and " + num2 + " is "
               + min(num1, num2));

       System.out.println("The maximum of " + num1 + " and " + num2 + " is "
               + max(num1, num2));

       System.out.println("The sum of " + num1 + " and " + num2 + " is "
               + sum(num1, num2));

       System.out.println("The difference of " + num1 + " and " + num2
               + ", , rounded to the nearest whole integer, is "
               + difference(num1, num2));

       System.out.println("The product of " + num1 + " and " + num2 + " is "
               + product(num1, num2));

       scan.close();

   }

   private static double product(double n1, double n2) {
       return n1 * n2;
   }

   private static double sum(double n1, double n2) {
       return n1 + n2;
   }

   // using the absolute value method and the round method from the Math class
   // to refine the results to the nearest whole positive integer.
   private static int difference(double n1, double n2) {
       double diff;
       diff = n1 - n2;
       diff = Math.abs(diff);
       int intDiff = (int) Math.round(diff);
       return intDiff;
   }
   private static double max(double n1, double n2) {
       if (n1 > n2) {
           return n1;
       } else {
           return n2;
       }
   }

   static double min(double n1, double n2) {
       if (n1 < n2) {
           return n1;
       } else {
           return n2;
       }
   }
}

Sample run :

Enter two numbers:
11.75 1.5
The minimum of 11.75 and 1.5 is 1.5
The maximum of 11.75 and 1.5 is 11.75
The sum of 11.75 and 1.5 is 13.25
The difference of 11.75 and 1.5, , rounded to the nearest whole integer, is 10
The product of 11.75 and 1.5 is 17.625

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote