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

We want to find the average of three positive numbers (doubles). Consider the fo

ID: 3703548 • Letter: W

Question

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 " , "Enter a positive number or type Q to terminate" while (true) one - getInput (input): // 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 whilee l // 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 Write the code that returns the smallest value and outputs that value 4.

Explanation / Answer

import java.util.Scanner;

class Lab5
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
double one;
double two;
double three;
System.out.printf("%s ","Enter a positive number or type -1 to terminate ");

while(true)
{
  one = getInput(input);
  if(one == -1)
  break;
  two = getInput(input);
  if(two == -1)
  break;
  three = getInput(input);
  if(three == -1)
  break;
  
  System.out.println("Minimum of three numbers : "+minimum(one,two,three));
}
}

public static double minimum(double one,double two,double three)
{
  
  double min = (one<two?(one<three?one:three):(two<three?two:three));//conditional operator
  return min;
}

public static double getInput(Scanner input)
{
  double x = input.nextDouble();
  if(x <0)
  return -1.0;
  else
  return x;
}
}

Output:

Enter a positive number or type -1 to terminate

14.5
12.9
4.7
-1

Minimum of three numbers : 4.7


Do ask if any doubt. Please upvote.

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