CMPU 101: Assignment 5

# CMPU 101 Assignment 5: Checksum Validation

Due by 11:59 PM on Thursday, March 9th

\$Revision: 1.1 \$

## Problem Specification:

A common requirement for software applications supporting inventory management is to validate the items being manipulated. For example, suppose a store manager wants to add a new item to the store's inventory database. To do so, the manager would need to insert a new entry into the database that gives the name and price of the item along with its code number. Correctly entering all three pieces of data is important, since an improperly entered code would make it impossible for price scanners to recognize the item.

One way to increase the likelihood of correct data entry is to use checksum software to test whether the digits of a potential code number have some particular property.

For example, there is a verification scheme for the Universal Product Code (UPC) numbers. The familiar UPC in bar code form is shown below.

A valid UPC number is a 12-digit value that meets the following criteria:

• Let m be the sum of the 2nd, 4th, 6th, 8th, and 10th digits (where the numbering of digits is from left to right.)

• Let n be the sum of the 1st, 3rd, 5th, 7th, 9th, and 11th digits.

• Let
```             r = 10 - (( m + 3 * n ) mod 10).
```

• A number meets the UPC encoding criteria if its 12th digit equals r.
Consider the number 780070121354.

For this number,

```             m = 8 + 0 + 0 + 2 + 3 = 13
n = 7 + 0 + 7 + 1 + 1 + 5 = 21
r = 10 - ((13 + 3 * 21) mod 10)
= 10 - (76 mod 10)
= 10 - 6
= 4
```
Because r = 4, the number corresponds to a possible UPC code.

Now consider the number 123456789011.

For this number,

```             m = 2 + 4 + 6 + 8 + 0 = 20
n = 1 + 3 + 5 + 7 + 9 + 1 = 26
r = 10 - ((20 + 3 * 26) mod 10)
= 10 - (98 mod 10)
= 10 - 8
= 2
```
Because r is not equal to 1, the 12th digit, the number does not correspond to a possible UPC code.

## Programming Assignment, Part I:

Write a Java application that does the following (method names shown in bold print):

```      0. getUPCCode
Prompt the user to enter a 12-digit number
While the number has the wrong number of digits,
Report an invalid entry and ask the user to re-enter the number
1. calculateCheckSum
Extract the digits into 12 separate integers and calculate m, n, and r
2. isValid
if r equals the 12th digit return true
else return false
3. main
getUPCCode();
calculateCheckSum();
if (isValid())
print "String [input String] is a valid UPC Bar Code."
else
print "String [input String] is not a valid UPC Bar Code."
```
Assume the user will enter only digits 0 through 9 and read the input into a String in step 0. You will need to convert each of the characters in this String to an int to do arithmetic on them in step 1. There are at least two ways to do this conversion:
1. Get each char out of the String and then use the static method in the Character class called "getNumericValue" to convert the char to an int.
2. Get each single letter substring from the String and then use the static method in the Integer class called "parseInt" to return the int value of the String.

Since this is a Java Application, you should also write a main method. The main method should be used to call other methods as shown above in step 3. You should declare all the methods and the non-local variables to be static.

## What you should hand in:

When your program compiles and run correctly, submit the Java source file using the CS 101 Submission WebsiteMake sure you submit the Java source file: the filename should end in ".java", not ".java~" or ".class".