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.

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

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:

  • 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).
    • oIf 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.
    • else, Calls the method printBarCode for the code entered
    • 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: 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.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.