Write the code that prompts the user to enter an integer


Assignment: Java Program

Input and Nested If Statements and Functions

This assignment is more exercise with conditional statements and code to make your program more robust. It builds on what you learned in the previous assignment in that you must now use else conditions and conditional blocks. This assignment should help you remember how to use if, else, blocks, and nested if statements in case you need a refresher.

The exercise is designed in a special way to show you how you should write large programs by adding a little bit at a time. Please do these Step s one at a time. The practice you will follow is called "refactoring."

Start by creating a function to get in integer from user input. Name that function, getUserValue_Step 1(). To show your work increments, implement each Step as a separate function, called getUserValue_Step 2(), getUserValue_Step 3(), getUserValue_setp4() and getUserValue_setp5(). Each time, copy the code from the previous Step , and then add the new modifications to the new function. Be aware that you will not always be adding to the end of the new function, and may even have to rearrange the order of the previous pieces, in order to satisfy the new expectations presented in one or more of the Step s.

To test your code, call the function for whichever Step you are working on from main().

Step 1. Write the code that prompts the user to enter an integer between 1 and 20 (including 1 and 20), reads the value using Scanner, and then prints the value that they entered in a statement that begins with "You entered a ".

Step 2. Add more code or modify your code so that if the user enters a value that is less than 1 or greater than 20, it prints out "The value you entered is not between 1 and 20." Otherwise, and only if the value is between 1 and 20, it prints the statement as before.

Step 3. Add more code or modify your code so that if the user enters the values 8, 11, or 18, the statement that is printed is "You entered an" instead of "You entered a". Note in putting these things in a particular order, that testing for 8, 11, and 18 makes more sense if you already know that the number is between 1 and 20.

Step 4. Add more code or modify your code so that if the user enters something that is not a number, i.e. includes letters, your program does not print any of the above lines, but instead prints out "Idiot! Your input could not be read as a number. Get a life!". Again, note that it doesn't make sense to test something to see if it is between 1 and 20 if it isn't even a number. So sequence your tests in a logical order of ruling out the various cases.

Step 5. Add more code or modify your code so that if the user enters something invalid or an integer not between 1 and 20 print invalid input and keep asking the user to input a number until a valid number between 1 and 20 is entered and then prints out the correct response.

Format your assignment according to the following formatting requirements:

1. The answer should be typed, double spaced, using Times New Roman font (size 12), with one-inch margins on all sides.

2. The response also include a cover page containing the title of the assignment, the student's name, the course title, and the date. The cover page is not included in the required page length.

3. Also Include a reference page. The Citations and references should follow APA format. The reference page is not included in the required page length.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Write the code that prompts the user to enter an integer
Reference No:- TGS02968060

Now Priced at $40 (50% Discount)

Recommended (98%)

Rated (4.3/5)