From Wikipedia:
A check digit is a form of redundancy check used for error detection on identification numbers, such as bank account numbers, which are used in an application where they will at least sometimes be input manually. It is analogous to a binary parity bit used to check for errors in computer-generated data. It consists of one or more digits computed by an algorithm from the other digits (or letters) in the sequence input.
A check digit in a number sequence, then, is used to attempt to verify that the other digits in the sequence are valid, and haven't been entered incorrectly.
As an example, here's the ISBN number for our Java textbook, Cay Horstmann's Java Concepts: Early Objects:
That ISBN number, 978-1-119-05650-8, consists of the code for the book—the digits 978111905650—along with a last digit, the 8, that can be algorithmically generated based on the values of the other digits.
If someone tries to order a book with an ISBN in which two of the digits have been unintentionally transposed, or if one of the digits is off, the check digit at the end of the sequence won't match with the check digit calculated by the algorithm. The incorrect ISBN number will be identified as one that is invalid.
The 16-digit (usually) number sequence used for ATM and credit cards uses a check digit. The Luhn algorithm is performed on the first 15-digits of the number, which is used to identify the final digit of the credit card. Any card whose number doesn't match the algorithm can be identified as having an invalid sequence. (Of course, just because a number passes the Luhn test doesn't guarantee that it is a valid number as far as the bank is concerned. Passing the Luhn test is "necessary but not sufficient" to indicate validity.)
The Vehicle Identification Number (VIN) for your vehicle, displayed on the front dash, uses a check digit as well.
Finally, Universal Product Codes (UPC) for retail items use a check digit to verify that they've been correctly scanned.
In this activity you'll be:
Do some research online to learn about the Luhn algorithm.
Make sure that you can work through determining the check digit of a 16-digit code manually before moving on to the next part of this assignment.
Write a class called CCChecker which has two methods:
Because these methods take the credit card number as a String, you'll need to convert those characters to int values.
In this activity you'll be:
Do some research online to learn about the UPC-A scheme.
Make sure that you can work through determining the check digit of a 12-digit code manually before moving on to the next part of this assignment.
Write a class called UPCChecker which has two methods:
Because these methods take the UPC code as a String, you'll need to convert those characters to int values.
You can test your methods using UPC-A codes found online, or use the sample items provided by the instructor in class.
We've been looking at the UPC-A code to this point. To allow for printing UPCs on a smaller package, the UPC-E scheme was developed. Calculating the check digit for this smaller code is a bit more complex than for the UPC-A above.
Research the UPC-E scheme, and write a new class, isValidE(String code), that takes a UPC-E code and returns true if the code is valid.