Open UserInput.java in DrJava. The program will ask the user to enter a range

 Open UserInput.java in DrJava. The program will ask the user to enter a range of values and then display them. 

Note that it includes commented out calls to the methods that you will define. 

Once you’ve implemented the relevant method you can uncomment those lines to test your implementation. 

Implement askForInt The new method askForInt is intended to prompt the user to enter an integer within a given range, read their response, and to continue prompting them and reading their response until it is within that range.

 Implement askForInt based on the following pseudocode description (where variable values appearing in output are highlighted): 

Method: int askForInt(Scanner in, String prompt, int min, int max) Returns: int, 

the valid value entered by the user Parameters: Scanner in, the Scanner to read user input String prompt, 

the message to prompt the user with int min, the lower bound of the allowed range int max, the upper bound of the allowed range Variables: 

int value, value entered by user 

Steps: 1 Display “prompt (min-max): ” on one line 

2 Assign value result of in.nextInt() 

3 While value < min or value > max: 

3-1 | Display “Invalid value” 

3-2 | Display “prompt (min-max): ” on one line 

3-3 | Assign value result of in.nextInt() 

4 Return value Note: Remember to use appropriate Java syntax for the while loop and logical or Add a suitable header comment to explain what this method does. 

Keep the description general; 

do not tailor it to suit the way it is used by the testing code in main. In the main() method remove the comment that reads in the value for the guess variable. 

Compile and run the program and test it by entering -50, then 11, before trying a valid value. 

Implement askForDouble Using the same pattern as above, design and implement askForDouble, and test using the percent value in main(). 

Hint: The changes are very slight, so copy, paste and modify. 

Don’t forget to modify the header comment as well. 

In the main() method remove the comment that reads in the value for the percent variable. 

Compile and run the program and test it with a variety of incorrect inputs before trying a valid value. 

Implement askForYesNo The Scanner’s nextBoolean() method does exactly what it claims—reads the next piece of text that is ‘true’ or ‘false’ and returns a boolean—and nothing more. 

So it’s fine for providing basic interaction, especially with other programmers, but it’s not a great way for a normal user to enter a truth value. 

Design and implement askForYesNo. 

You can use the previous two methods as a guide but make the following key changes: Remove the min and max parameters. Knowledge of valid values will be built-in to the method. 

Display the prompt followed by suitable explanatory text such as ‘(yes/no)’. Do not use nextBoolean(), since it only works with the inputs ‘true’ and ‘false’. Instead use the Scanner’s next() method to read in the next String the user enters. If the user enters “y”, “yes” or “true” (in any case) then the method should return true. If the user enters “n”, “no” or “false” (in any case) then the method should return false. If they enter any other value then display an error and prompt them again, as done in askForInt and askForDouble. 

Tips: Use the String method toLowerCase() to convert the user’s input to a value suitable for comparing against the valid alternatives. (As with all String methods, it returns a copy of the modified String, it does not actually change the String on which it is called.) 

You can use either an if or switch construct for checking (and converting) the user’s input. 

Consider declaring two boolean variables in your method, one the value to be returned and another indicating if the current input is actually valid. 

Consider using a do-while loop (and the ‘is valid’ variable suggested above) as this will reduce the amount of code you need to write. As with the previous two methods, add a method header comment containing a general description of the method’s behaviour (not a description of the example usage shown in main). 

In the main() method remove the comment that reads in the value for the again variable. Compile and run the program and test it with a variety of incorrect inputs before trying a valid value. Remember that upper case or mixed case entries are valid if they match any of the six possible input values. 

Tags: No tags