Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a java program that determines whether a four-digit numeric code entered by users of a laboratory door are valid, and prints the appropriate messages.

Write a java program that determines whether a four-digit numeric code entered by users of a laboratory door are valid, and prints the appropriate messages.

The program should prompt the user for a four-digit number, read the number, and determine whether if it is valid.

The input is a single integer, not a string, and not multiple integers.

Valid numbers must be four digits (0 through 9) with a correct check digit. The check digit is the 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 numeric code 9851 is valid, we compute the sum of the three left most digits (9 + 8 + 5 = 22) and then compute the remainder of the sum divided by 7 (22%7=1). Since the remainder is 1 and is equal to the check-digit which is 1, the ID 9851 is a valid ID.

The program will print an error message if:

  • the code has 3 or less digits
    • Displays error message: ***Invalid ID number. Too few digits. Goodbye!
  • more than 4 digits
    • Displays error message: ***Invalid ID number. Too many digits. Goodbye!
  • code has 4 digits but does not have a correct check digit.
    • Displays error message: ***Invalid ID number. Check digit is invalid. Goodbye

The program will print ***Your ID is valid. You are authorized to use this door if the code is valid.

The program quits if the user enters a negative 1 (-1) and displays Goodbye!

You can use / and % operators to extract the individual digit.

For example, to extract the first digit from 9851, divide 9851 by 1000 will give you 9. This is a integer division so the result is the integer 9. To extract the 8, divide the remainder (851) by 100. That should give you 8. To extract the 5, divide the remainder (51) by 10. That should give you 5.

Make sure your program does the following:

  1. Checks for -1 to end program
  2. Checks for too few digits
  3. Checks for too many digits
  4. Extracts first digit
  5. Extracts second digit
  6. Extracts third digit
  7. Extracts fourth digit
  8. Checks for valid check digit.
  9. Prints appropriate error messages or valid messages

Examples of 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 doesnt match the sum. Goodbye!

BUILD SUCCESSFUL (total time: 19 seconds)

run:

Please enter a four-digit identification number (-1 to end): 9851

***Your ID is valid. You are authorized to use this door.

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)

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

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

What is the output of the following code? a = 1 0 1 ? 3 print ( a )

Answered: 1 week ago