Concepts explored
Reference: This assignment is based on the textbook's programming exercise P5.22 (pages 254 255). Please read the description given there thoroughly. The problem description given here differs in some respects from the textbook's version, so read this document carefully also.
Description
This program will prompt for, and read 5 digit Zip codes from the user (using a sentinel control loop), and display the bar codes for valid Zip codes entered as shown in the book. If the user enters something that is not a valid 5 digit Zip code then the program should just give an error message telling the user what a valid zip code must look like. The loop will terminate when the user enters the sentinel value "Quit".
Validation rules
The user must enter exactly 5 digits for the Zip code and no other character(s). Read the user's entry into a String variable (and not an int variable as the book's description calls for) . Check the following to validate the data as a valid Zip code:
The data entered is NOT a valid Zip code if either of the above tests fails.
Your program should have the following functions (methods) besides "main". The method headers and the work carried out by the methods are described next.
static void printDigit(int d)
This function prints the bar code corresponding to the digit d. Note that you can ignore the weighting scheme for the bits shown on page 254. You just need to print the appropriate pattern of '|' and ':' as a 5 character string which you can figure out from the table. For example, the digit 0 is to be translated as "||:::" and the digit 3 is to be translated as "::||:".
You can implement the translation by a sequence of if statements as in the function digitName on page 233, but a better alternative is to define an array of size 10 containing the bar code strings for each digit. Then the code for a digit d is simply the array element at index d!
Note: Do not use println in this method because all 6 barcodes must be side by side. Also, to make it easier to check that the bar code printed is correct, we will print a space after the first framing bar and after every digit code. See sample output.
static void printBarCode (String zipCode)
This method is only called with validated zip code. It first prints the left frame bar and a space, then it uses (calls) the function printDigit for each of the 5 digits in the code (stored as characters). It also calculates the sum of those 5 digits and uses that to generate the check digit. The check digit must be such that it added to the sum of the code digits is a multiple of 10. The barcode for the check digit and the right frame bar is printed and the line ended (so here we use println).
static boolean isValidZip(String code)
This method returns false if the string parameter is not a valid zip code as described earlier. It returns true if the parameter is a valid zip code.
public static void main(String [] args)
This function does the following:
Program organization
The program uses several functions so that each function solves one well defined part of the overall problem. Java library functions are also used.
Here is a structure diagram to show which functions call which other functions: see image.
Calculating the numerical value of a digit character (method char2int)
You can extract the digit characters from a string using the .charAt String class function, but the digit characters cannot be used as subscripts or for computation. You must convert the character value to the corresponding integer value. This can be done using the following code snippet:
char ch = (some digit character);
int d = (int) ch – (int) '0';
Note: If you define the digit to bar code translation array, define it as a local variable in the function printDigit, or define it as a static variable at the class level. Any computational need in a function should be met by adding local variables in the functions. The standard final static variables can be used however, (provided in start-up code).
A sample output follows: see image.