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

in java Write a program that: Repeatedly asks the user to enter two real numbers

ID: 672607 • Letter: I

Question

in java

Write a program that: Repeatedly asks the user to enter two real numbers. It stops when the numbers entered by the user are equal. Counts how many pairs were entered (the last pair should not be counted). For each pair of numbers it computes the 'distance' between them. The distance will be computed as the absolute value of their difference (the distance should always be a positive number). Use the Math.abs function to get the absolute value. At the end prints the smallest distance (out of all the distances computed based the numbers from the user). The distance between the last two numbers should not be included in computing the minimum (it will be 0 since these numbers are equal). Keep track of the minimum yourself by using a variable that remembers what is the smallest distance you have seen. Whenever you get a new distance compare it with that variable and update the variable if the new distance is smaller. If the user enters two equal numbers from the beginning, the count should be 0 and the minimum should also be 0.

Explanation / Answer

import java.util.Scanner;

public class hw3_task3 {

   public static void main(String args[]) {

       try {
           double minDistance = 0.0;
           int pair = 0;
           Scanner scanner = new Scanner(System.in);
           while (true) {

               System.out.print("Enter Number 1: ");

               double num1D = scanner.nextDouble();
               System.out.print("Enter Number 2: ");

               double num2D = scanner.nextDouble();

               if (num1D == num2D) {
                   break;
               } else {
                   pair++;
                   if (minDistance == 0.0) {
                       minDistance = Math.abs(Math.abs(num1D)
                               - Math.abs(num2D));

                   } else if (minDistance > Math.abs(Math.abs(num1D)
                           - Math.abs(num2D))) {
                       minDistance = Math.abs(Math.abs(num1D)
                               - Math.abs(num2D));

                   }

               }

           }
           System.out.println("You entered " + pair + " pairs");
           System.out.println("the minimum distance is: " + minDistance);

       } catch (Exception e) {

       }

   }

}

........Sample Run 1(minimum of a few distances is computed)

Enter Number 1: 10
Enter Number 2: 3
Enter Number 1: 5
Enter Number 2: 3
Enter Number 1: 6
Enter Number 2: 8
Enter Number 1: 2
Enter Number 2: 2
You entered 3 pairs
the minimum distance is: 2.0

........Sample Run 2(0 pairs)

Enter Number 1: 2
Enter Number 2: 2
You entered 0 pairs
the minimum distance is: 0.0

........Sample Run 3(real numbers)
Enter Number 1: 14.5
Enter Number 2: 16.8
Enter Number 1: 3
Enter Number 2: 3
You entered 1 pairs
the minimum distance is: 2.30

........Sample Run 4(number 1 larger than number 2)
Enter Number 1: 9
Enter Number 2: 1
Enter Number 1: 2
Enter Number 2: 2
You entered 1 pairs
the minimum distance is: 8.0