Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write methods that both prompt the user and use a Scanner to read user input with some amount of validation of their input. Specifically, you

Write methods that both prompt the user and use a Scanner to read user input withsomeamount of validation of their input. Specifically, you will write methods to:

  • prompt for and read anintwithin a given range (e.g., between 1 and 10);
  • prompt for and read adoublewithin a given range (e.g., between 0.0 and 1.0); and
  • prompt for and read abooleanbut accept a wider range of English-language truth values (y, n, yes, no, true, false) than those allowed by the Scanner'snextBoolean()method.
  1. User Input Functions Starter Code

import java.util.Scanner;

/**

* 4.3CR User Input Functions

*

* @author Your name here

*/

public class UserInput {

//TO DO: Define promptForInt here

//TO DO: Define promptForDouble here

//TO DO: Define promptForYesNo here

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

//All values are initialised so code will compile and run

int guess = -1;//user's guess (between 1 and 10)

double percent = -1;//a percentage (as a value between 0 and 1)

boolean again = false; //do they want to go again?

System.out.println("Testing prompt for int... the number should be saved in guess.");

System.out.println(" - Enter '50' -- should loop with error");

System.out.println(" - Enter '-5' -- should loop with error");

System.out.println(" - Enter '5' and it should work");

// guess = promptForInt(sc, "Enter a number", 1, 10);

System.out.println("Guess: " + guess);

System.out.println();

System.out.println("Testing prompt for double... the number should be saved in percent.");

System.out.println(" - Enter '50' -- should loop with error");

System.out.println(" - Enter '-1' -- should loop with error");

System.out.println(" - Enter '0.5' and it should work");

// percent = promptForDouble(sc, "Enter percent value", 0.0, 1.0);

System.out.println("Percent: " + percent);

System.out.println();

System.out.println("Testing prompt for yes/no... the result is saved in again.");

System.out.println(" - Extend these boolean tests... add messages and verify your solution!");

System.out.println(" - Enter 'yes' and it should succeed");

// again = promptForYesNo(sc, "Play again?");

System.out.println("Again: " + again);

System.out.println();

System.out.println(" - Verify that it can also read in false...");

// again = promptForYesNo(sc, "Play again?");

System.out.println("Again: " + again);

System.out.println();

System.out.println("Tests complete...");

}

}

  1. The new method promptForInt 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.
  2. ImplementpromptForIntbased on the following pseudocode description (where variable values appearing in output arehighlighted):
Method: int promptForInt(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 
  1. Note:Remember to use appropriate Java syntax for thewhileloop and logicalor
  2. Add a suitable header comment to explain what this method does. Keep the descriptiongeneral; do not tailor it to suit the way it is used by the testing code in main.
  3. In the main() method remove the comment that reads in the value for theguessvariable.
  4. Compile and run the program and test it by entering -50, then 11, before trying a valid value.

Implement promptForDouble

  1. Using the same pattern as above, design and implementpromptForDouble, and test using thepercentvalue in main().
  2. Hint:The changes areveryslight, so copy, paste and modify. Don't forget to modify the header comment as well.
  3. In the main() method remove the comment that reads in the value for thepercentvariable.
  4. Compile and run the program and test it with a variety of incorrect inputs before trying a valid value.

Implement promptForYesNo

The Scanner'snextBoolean()method does exactly what it claimsreads the next piece of text that is 'true' or 'false' and returns abooleanand 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.

  1. Design and implementpromptForYesNo. You can use the previous two methods as a guide but make the following key changes:
  • Remove theminandmaxparameters. Knowledge of valid values will be built-in to the method.
  • Display thepromptfollowed by suitable explanatory text such as '(yes/no)'.
  • Do not usenextBoolean(), since it only works with the inputs 'true' and 'false'. Instead use the Scanner'snext()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 returntrue.
  • If the user enters "n", "no" or "false" (in any case) then the method should returnfalse.
  • If they enter any other value then display an error and prompt them again, as done inpromptForIntandpromptForDouble.
  1. Tips:
  • Use the String methodtoLowerCase()to convert the user's input to a value suitable for comparing against the valid alternatives. (As with all String methods, it returns acopyof 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 twobooleanvariables 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.
  1. In the main() method remove the comment that reads in the value for theagainvariable.
  2. 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.
  3. Take a screenshotof one run of your program, including entering several incorrect values, and upload both the UserInput.java source file and screenshot to the assignment submission folder on MyLO.

  1. Use UserInput.java

The program ask the user to enter a range of values and display them. Note : 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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions