Question
Write a Java program named Prompter which prompts for numeric input and displays the result. Your program must include the following: A method which prompts
Write a Java program named "Prompter" which prompts for numeric input and displays the result.
Your program must include the following:
- A method which prompts the user for an integer in a specified range and returns the user's input as an integer to the main method. Your method must accept a Scanner and two int variables as parameters. The int parameters represent the minimum and maximum range allowed as input. Input must be validated against this range.
- A second method which overloads the first by prompting the user for a double value in a specified range and returns the user's input as a double to the main method. Your method must accept a Scanner and two double variables as parameters. The double parameters represent the minimum and maximum range allowed as input. Input must be validated against this range. Floating point output should be displayed with 2 significant places after the decimal point.
You must use the following main method with no modifications in your program:
public static void main(String[] args) { final int MININT = 1; final int MAXINT = 100; final double MINDOUBLE = 5.0; final double MAXDOUBLE = 50.0; Scanner in = new Scanner(System.in); // prompt for an int and double and display System.out.printf("int result: %d ", prompt(in, MININT, MAXINT)); System.out.printf("float result: %.2f ", prompt(in, MINDOUBLE, MAXDOUBLE)); }
Note: Notice that the MIN/MAX values are declared as constants in the main method above. Because Java uses pass by value for parameters, these values could be modified in the methods if not also declared as constants, so be sure to declare them as final in your methods. In other words, your first method could be declared as follows:
public static int prompt(Scanner in, final int MIN, final int MAX)
Expected output (user input shown in red):
Please enter an integer between 1 and 100: 200 Invalid value, please try again. Please enter an integer between 1 and 100: -10 Invalid value, please try again. Please enter an integer between 1 and 100: 25 int result: 25 Please enter a floating point value between 5.0 and 50.0: 75.5 Invalid value, please try again. Please enter a floating point value between 5.0 and 50.0: 2.0 Invalid value, please try again. Please enter a floating point value between 5.0 and 50.0: 34.771 float result: 34.77
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started