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

Lab 5 – Introduction to Methods We want to find the average of three positive nu

ID: 3703245 • Letter: L

Question

Lab 5 – Introduction to Methods We want to find the average of three positive numbers (doubles). Consider the following class (which is not complete): // Program finds the minimum of 3 positive numbers import java.util.Scanner; public class Lab5 { // find the minimum of three numbers public static void main(String[] args) { Scanner input = new Scanner(System.in); double one; // first number double two; // second number double three; // third number System.out.printf("%s %s", "Enter a positive number or type Q to terminate"); while (true) { // Write code to get the remaining inputs and // if a negative value is entered. // Write code to display the minimum of the three // floating-point numbers */ } // end while } // end main // the minimum method determines the smallest of three numbers // complete the header for the minimum method // that takes three double parameters and returns a double public static minimum { // Add code to compute and return the minimum of three numbers } // end method minimum // the getInput method prompts the user for input and // returns a number entered by the user or -1 if a negative number // or if something else is entered. public static double getInput(Scanner input) { // Add code to prompt and return the user's input } // end method getInput } 1. Complete the getInput method to prompt the user and return the user’s input or a -1 if he enters something other than a number. 2. Add the appropriate code in the while loop to input the remainder of the input data using the getInput method or exiting the program whenever a negative value is returned. 3. Complete the header of the three parameter minimum method shown above. 4. Write the code that returns the smallest value and outputs that value. 5. Create a new method, minimum, that returns the smallest of two doubles. 6. Complete the three parameter minimum method the use the two parameter minimum method.

Explanation / Answer


//Source code to copy
//Lab5.java
import java.util.Scanner;
public class Lab5
{
   // find the minimum of three numbers
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);
       boolean repeat=true;
       String choice;
       double // first number
       double two=0; // second number
       double three=0; // third number
       System.out.printf("%s ",
               "Enter a positive number or type Q to terminate");
       choice=input.nextLine();

       if(!choice.equals("Q"))
          >

       while (repeat)
       {
           two = getInput(input);
           three = getInput(input);
           System.out.printf("Minimum : %f ",minimum(one, two, three));
           System.out.printf("%s ",
                   "Enter a positive number or type Q to terminate");
           choice=input.nextLine();
           if(!choice.equals("Q"))
                      
           else
               repeat=false;

       } // end while

       System.out.println("Thank you!");
   }// end main

   /* minimum method determines the smallest of three numbers
   * complete the header for the minimum method
   * that takes three double parameters and returns a double */
   public static double minimum(double n1,double n2,double n3)
   {
       double smallest=minimum(n1, n2);
       smallest=minimum(smallest, n3);

       return smallest;

   } //end method minimum
   /*minimum method that takes two double and returns the
   * smallest of two double values*/
   public static double minimum(double n1,double n2)
   {
       if(n1<n2)
           return n1;
       else
           return n2;
   } // end method minimum
   /* the getInput method prompts the user for input and
   * returns a number entered by the user or -1 if a negative number
   * or if something else is entered. */
   public static double getInput(Scanner input)
   {
       double number;
       // Add code to prompt and return the user's input
       try {
           System.out.printf("Enter a number : ");
           number=Double.parseDouble(input.nextLine());

       } catch (Exception e) {
           return -1;
       }

       return number;

   } // end method getInput
}

Sample Output:

Enter a positive number or type Q to terminate
1
Enter a number : 2
Enter a number : 3
Minimum : 1.000000
Enter a positive number or type Q to terminate
Q
Thank you!