Write a program that determines whether identification numbers typed by users of a photocopier are valid, and
Fantastic news! We've Found the answer you've been seeking!
Question:
- Write a program that determines whether identification numbers typed by users of a photocopier are valid, and prints the appropriate messages shown in the sample runs below.
- The program should prompt the user for a four-digit identification number, read the number, and determine whether it is valid. The input is a single integer, not a string, and not multiple integers. Valid numbers must be 4 digits with a correct "check digit." The check digit for an identification number is its rightmost digit; a correct check digit is equal to the remainder of the sum of the three left most digits divided by 7.
- For example, to determine if the ID 5731 is valid, we compute the sum of the three left most digits (5 + 7 + 3 = 15) and then compute the remainder of the sum divided by 7. Since the remainder is 1 and is equal to the check-digit, which is also 1, the ID 5731 is a valid ID.
- The program will print an error message if the number has less than 3 or more than 4 digits or if it has 4 digits but does not have a correct "check digit." The program quits if the user enters a negative 1.
- Use / and % operators to extract the individual digit. For example, to extract the first digit from 5731, divide 5731 by 1000 will give you 5. This is an integer division, so the result is the integer 5. To extract the 7, divide the remainder of 5731 by 1000. That should give you 7. The logic for extracting the rest of the digits is similar.
- Submission: Upload your .java file for Program 1 to Blackboard.
- Sample runs
- run:
- Please enter a four-digit identification number (-1 to end): 123 ***Invalid ID number. Too few digits. Goodbye!
- BUILD SUCCESSFUL (total time: 19 seconds)
- run:
- Please enter a four-digit identification number (-1 to end): 12345 ***Invalid ID number. Too many digits. Goodbye!
- BUILD SUCCESSFUL (total time: 19 seconds)
- run:
- Please enter a four-digit identification number (-1 to end): 1234
- ***Invalid ID. Check digit doesn't match the sum. Good bye!
- BUILD SUCCESSFUL (total time: 19 seconds)
- run:
- Please enter a four-digit identification number (-1 to end): 5731 ***Your ID is valid. You are authorized to use this copier.
- BUILD SUCCESSFUL (total time: 19 seconds)
- run:
- Please enter a four-digit identification number (-1 to end): -1 Goodbye!
- BUILD SUCCESSFUL (total time: 19 seconds)
Related Book For
Posted Date: