Risk Management Report
December 3, 2018
Statistics & Computing
December 3, 2018

Assignment 6 Zip code to Bar code conversion

Concepts explored
 Data validation
 Functions (methods)
 Use of arrays for code translation
 Some String operations (including equality comparison)
 Indefinite loops (sentinel control)
 Check digit generation
 Summing and counting
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.
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)1. Check the following to validate the data as a valid
Zip code:
 The length of the string is 5
 Each character in the string is a digit (can use Character.isDigit(), see section 7.2.3, p. 338).
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)2
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!
1 There are ZIP codes that have a leading 0. But if you read this into an int variable, then you cannot distinguish between 01234 and
2 The code for this method is going to be just one line (print the Barcode by accessing the translation array, and print a space). So it is
ok to not have this method. In that case its work is done in printBarCode().Page 2 of 3 Updated: 2019-11-26
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
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
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:
 Prints the assignment number and student’s name
 Prints short description of the program (could be done by calling a method)
 Prompts for and gets the first zip code (could be the sentinel) where the data is supposed to be a 5-digit zip
code, the sentinel value being the word “Quit” (without the quote marks).
 While the code is not sentinel (Use String class method .equalsIgnoreCase so the user can enter the sentinel
without worrying about what combinations of upper- and lower-case letters are acceptable).
o If the zip code is not valid
 Prints a message telling the user what a correct zip code looks like. Since this message might
have quite a few lines of text, you may consider writing a method to display the message, and
just calling the method in ‘main’. This will help keep the ‘main’ method short.
o else
 Calls the method printBarCode for the code entered
o Prompt for and get the next zip code
 End loop
 Once the loop is over the method prints the “Assignment complete” message and terminates
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:
isValidZip printBarCode
char2int printDigit
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);Page 3 of 3 Updated: 2019-11-26
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).
Program style requirements: The program must contain a header comment block, each major section of the program
must be identified by comments (as in the labs) and the statements should line up properly. See the formatting in the
book or my sample programs. Both the program and the results should be nice to read.
To get full credit for the assignment, you must do the following:
 Use variable names that are meaningful
 Header block comments and section comments should be given
 Program text should be easy to read
 No extra-long lines in the program code.
 Program output should be easy to read and well –organized.
 Output window should be easy to read. Make it look like the sample output given.
A sample output follows:

code to be used:
final static Scanner cin = new Scanner(System.in);
final static String[] BAR_CODES = {"||:::", ":::||", "::|:|", "::||:",
":|::|", ":|:|:", ":||::", "|:::|", "|::|:", "|:|::"

public static void main(String[] args) {

// TODO: add things here

} // end main

static void printBarCode(String zipcode) {

// print first framing bar and a space
out.print("| ");

// TODO: print bar codes for each digit using the translation table
// also add up the digits of the zip code

// TODO: calculate check digit and print its code

// print last framing bar and end the line
} // end method

static void printDigit(int digit) {

// TODO: print the appropriate bar code and a space

} // end method

static boolean isValidZip(String zipCode) {

// TODO: check length, return false if not 5 characters

// TODO: for each of the 5 characters check that it is a digit
// if not, return false

return true;
} // end function

private static int char2int(char c) {
if (! isDigit(c))
terminate("char2int: argument is not a digit character");
return (int) c – (int) ‘0’;
} // end char2int

private static void terminate(String message) {
out.println(message + "\nProgram terminated");
} // end terminate

Leave a Reply

Your email address will not be published. Required fields are marked *

My Account