Complete that program, by defining a pickMiddle function, and by modifying the e
ID: 3923987 • Letter: C
Question
Complete that program, by defining a pickMiddle function, and by modifying the existing userDouble function:
pickMiddle takes three arguments, called first, second, third.
pickMiddle(first, second, third) returns the middle of the values of the three arguments.
Function userDouble takes one argument, called message, which is a string.
Function userDouble prints out the message, and then enters a loop, where:
The program waits for the user to enter a double number.
If the user enters an invalid number, the function prints that what the user entered "is not a valid double".
If the user enters "q" or "Q", then the program exits (the code provided already takes care of that).
If the user enters indeed a valid double number, the function returns that number.
Explanation / Answer
Middles.java
import java.util.Scanner;
public class Middles
{
// This function gets a double number from the user.
// It ensures that the user enters a valid double number.
public static double userDouble(String message)
{
//Scanner class Object is sused to read the inputs entered by the user.
Scanner in = new Scanner(System.in);
double result = 0.0;
while (true)
{
//getting the String entered by the user
System.out.printf(message);
String s = in.next();
/*Checking whether the string is equal to "q" or not
* If yes,program will exit
*/
if (s.toLowerCase().equals("q"))
{
System.out.printf("Exiting... ");
System.exit(0);
}
/* Parsing the String to double
* If that value is not double type
* It will Throw Exception.
* And prompts user to enter again.
*/
try
{
result=(Double.parseDouble(s));
}
catch(Exception e)
{
System.out.println(":: Is not a valid double ::");
continue;
}
return result;
}
}
public static void main(String[] args)
{
//This loop continues to iterate as until user press 'q' or 'Q'
while (true)
{
double first = userDouble("please enter the first number, or q to quit: ");
double second = userDouble("please enter the second number, or q to quit: ");
double third = userDouble("please enter the third number, or q to quit: ");
//Calling the method pickMiddle() by passing the arguments
double middle = pickMiddle(first, second, third);
System.out.printf("the middle value is %.1f ", middle);
}
}
//This method finds and returns the middle value of three numbers
private static double pickMiddle(double first, double second, double third) {
//Declaring local variable
double middle=0.0;
//If this condition satisfies then middle is first number
if(first>second && first<third || first<second && first>third)
{
middle=first;
}
//If this condition satisfies then middle is second number
else if(second>first && second<third || second<first && second>third)
{
middle=second;
}
//If this condition satisfies then middle is third number
else if(third>second && third<first || third<second && third>first)
{
middle=third;
}
return middle;
}
}
__________________________________________
Output:
please enter the first number, or q to quit: t
:: Is not a valid double ::
please enter the first number, or q to quit: 56.67
please enter the second number, or q to quit: 93.56
please enter the third number, or q to quit: 45.56
the middle value is 56.7
please enter the first number, or q to quit: 12.23
please enter the second number, or q to quit: 78.65
please enter the third number, or q to quit: 45.67
the middle value is 45.7
please enter the first number, or q to quit: q
Exiting...
____________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.