This program will add three integers that the user inputs. The first value must be between 0 and 50. The second value must be between 51 and 100. The third value must be greater than 100. Your program should use try/catch blocks and loops to protect against input mismatches and to get a value within the above ranges. You MUST use custom exceptions for the input range. I know this could also be accomplished using if/else statements, but I want you to use try/catches for this. You MUST obtain each number from the user one at a time and make sure it is acceptable before moving on. This may require the use of THREE loops, each with its own request for input and try/catch blocks. Once you have three good values, print the sum of them to the screen. You will receive no credit if you make the user input all three numbers multiple times. (See Incorrect Samples)
-
This
program will add three integers that the user inputs. The first value must be between 0 and 50. The second value must be between 51 and 100. The third value must be greater than 100. -
Your program should use try/catch blocks and loops to protect against input mismatches and to get a value within the above ranges.
-
You MUST use custom exceptions for the input range. I know this could also be accomplished using if/else statements, but I want you to use try/catches for this.
-
You MUST obtain each number from the user one at a time and make sure it is acceptable before moving on. This may require the use of THREE loops, each with its own request for input and try/catch blocks.
-
Once you have three good values, print the sum of them to the screen.
-
You will receive no credit if you make the user input all three numbers multiple times. (See Incorrect Samples)
CORRECT SAMPLE OUTPUT: (Notice how we do not move on from an input until we get something acceptable.)
This program will add three numbers. .
Please enter number 1 (between 0 and 50):
>>>-4
Invalid input. Number must be between 0 and 50.
Please enter number 1 (between 0 and 50:
>>>10
Please enter number 2 (between 51 and 100):
>>>nine
Must be an integer.
Please enter number 2 (between 51 and 100:
>>>60
Please enter number 3 (greater than 100):
>>>2.4
Must be an integer.
Please enter number 3 (greater than 100):
>>>200
The sum is 270.
Goodbye.
INCORRECT SAMPLE OUTPUT: (This does not tell us where the problem is and requires us to enter all three numbers in again.)
This program will add three numbers. The first must be between 0 and 50. The second between 51 and 100. The third greater than 101.
Please enter your three numbers:
>>>7
>>>6
>>>2.5
Invalid input.
Please enter three numbers:
Etc.
INCORRECT SAMPLE OUTPUT: (This has us enter one value at a time, but doesn’t catch the error immediately. When there’s an error, it makes us start over.)
This program will add three numbers. The first must be between 0 and 50. The second between 51 and 100. The third greater than 101.
Please enter number 1:
>>>1
Please enter number 2:
>>>6
Please enter number 3:
>>>2.5
Invalid Input.
Please enter number 1:
Etc…
with JAVA
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images