# CECS 277 – Lab 2 – Methods solution

\$30.00

Original Work ?

5/5 - (1 vote)

## Postal Bar Code Printer

The USPS prints a bar code representing the
delivery zip code on most letters for quicker
mail sorting purposes, which looks similar to
the example to the right:

Create a program that prompts the user for a zip code and then prints out the bar code for
that zip code in a similar way. Use the character ‘|’ for the long bars and the character
‘.’ for the short bars. The bar code is made up of the following: one frame bar to signify
the start of the bar code, the five digits of the zip code (where each digit has its own
string of five bars), the check digit, and finally an ending frame bar.

See example below:
Each digit, including the check digit is represented by the following table:
Digit 1 2 3 4 5 6 7 8 9 0
Bar
Code …|| ..|.| ..||. .|..| .|.|. .||.. |…| |..|. |.|.. ||…

The check digit is calculated by doing the following:
1. Sum each digit in the zip code.
2. Round up the sum to the next multiple of 10.
3. Subtract the sum from the rounded value.
4. The result of the subtraction is your check digit.
Example: For zip code 90840: 9+0+8+4+0 = 21. 30-21 = 9. The check digit is 9.

Create the following methods in your program:
1. String getZip() – gets the user’s input for the zip code (String or an int).
Repeatedly check the input until the user has entered a valid 5-digit value that is
between 10000 and 99999. Then return it as a String.
2. void printDigit(char d) – prints the bar code for the single digit d.

3. char getCheckDigit(String zip) – calculates the check digit.
4. void printBarCode(String zip) – prints the bar code for the zip code
by iterating through the string and calling printDigit, calling getCheckDigit, as
well as printing the frame bars.
Example Output:
Enter a zip code: 90840
||.|..||…|..|..|..|||…|.|..|
Enter a zip code: 95014
||.|…|.|.||……||.|..|…|||