Input and Nested If Statements and Functions Assignment | Homework Help Websites
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 steps 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_step1(). To show your work increments, implement each step as a separate function, called getUserValue_step2(), getUserValue_step3(), 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 steps.
To test your code, call the function for whichever step you are working on from main().
step1. 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 “.
step2. 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.
step3. 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.
step4. 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.
Step5. 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.
Include comments in your code to explain what each of the ‘if’ statements is testing.
Zip the java file in a single zip file and submit the zip file.