Check Digits

Overview

  1. Introduction
  2. Credit Card check digits
  3. The Luhn Algorithm
  4. The UPC Check Digit Scheme
  5. Writing the UPC Checker

Introduction

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.

ISBN Numbers

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.

 

Credit Card numbers

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.)

VIN numbers

The Vehicle Identification Number (VIN) for your vehicle, displayed on the front dash, uses a check digit as well.

UPC Codes

Finally, Universal Product Codes (UPC) for retail items use a check digit to verify that they've been correctly scanned.

The Luhn Algorithm check digit strategy

In this activity you'll be:

  1. learning the check digit scheme for ATM and credit card numbers
  2. writing a credit card number checker
  3. using your credit card number checker to determine check digits for a number of cards

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.

Problem: Write the CCChecker class

Write a class called CCChecker which has two methods:

  1. checkDigit(String code) is an int method that calculates the check digit for a 15-digit credit card number according to the Luhn algorithm.
  2. isValid(String code) is a boolean method that returns true if a given credit card number is valid.

Because these methods take the credit card number as a String, you'll need to convert those characters to int values.

Show/hide how

int foo = Integer.parseInt("1234");

The UPC Check Digit Scheme

In this activity you'll be:

  1. learning the check digit scheme for UPC codes on items that you buy in the store
  2. writing a UPC code checker
  3. using your UPC code checker to determine check digits for a number of items

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.

Problem: Write the UPCChecker class

Write a class called UPCChecker which has two methods:

  1. checkDigit(String code) is an int method that calculates the check digit for a UPC-A code.
  2. isValid(String code) is a boolean method that returns true if a given UPC-A code is valid.

Because these methods take the UPC code as a String, you'll need to convert those characters to int values.

How do you do that?

int foo = Integer.parseInt("1234");

Using the Checker

You can test your methods using UPC-A codes found online, or use the sample items provided by the instructor in class.

Good Things in Small Packages

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.