When you receive a letter in the mail, take a look at the envelope. At the bottom of the envelope is a series of long and short bars. These bars represent the zip code for the city in which the letter is delivered. For example, if your name were Flicka and you lived in Washington, D.C., letters to your home might look like this:
![]() |
![]() |
Each digit of the zip code is represented by five bars. The first line and last lines are frame bars and do not count. Note the picture uses a 9 digit zip code. For this project you will only have to deal with 5 digit zip codes. |
The last five bars before the frame bar are the check digit. The check digit is calculated by adding up the digits of the zip code and seeing what would have to be added to the sum to get a multiple of 10. For example, the check digit for the zip code 51220 would be 0 (as 5 + 1 + 2 + 2 + 0 = 10), while the check digit for 82125 would be 2 (as 8 + 2 + 1 + 2 + 5 = 18).
Design and implement a program for translating 5 digit ZIP codes into Postal Bar Codes and vice versa. Your program will use the symbols: | for full bars and a . for half bars.
For your program we will assume a ZIP code consists of five consecutive digits. A Postal Bar Code consists of five digits encoded using vertical bars and dots, including the frame bars and the check digit.
For example, the following are both legal input for your program:
90210
||.|..||.....|.|...||||...|..|.|
Your program should print an error message if an input line does not contain a legal ZIP code or PostalBar Code
/** * Class BarCode: >>> Code is NOT complete !!! <<< */ public class BarCode { private static String barCodeInput; private static int zipCodeInput; /** * takes a string bar code as input and returns a 5-digit zip code */ public static String translate(String code) { barCodeInput = code; return barToNum(); } /** * takes a 5-digit zip code as input and returns a string zip code */ public static String translate(int code) { zipCodeInput = code; return numToBar(); } /** * a method which converts a bar code string into a 5 digit zip code */ private static String barToNum() { // >>>>>>>> complete this method <<<<<<<<< } /** * a method which converts a 5 digit zip code into a bar code string */ private static String numToBar() { // >>>>>>>> complete this method <<<<<<<<< } // >>>>> add other helper methods as needed <<<<<< }
/** * Tester for BarCode program: * Make sure you get the output indicated in the comments below. * * >>> OUTPUT FROM TESTER <<< * * 90210 * ||.|..||.....|.|...||||...|..|.| * 12561 * |...||..|.|.|.|..||.....||.|.|.| * error * error * error * error * error * */ public class BarCodeTester { public static void main( String[] args) { System.out.println(BarCode.translate("||.|..||.....|.|...||||...|..|.|")); System.out.println(BarCode.translate(90210)); System.out.println(BarCode.translate("|...||..|.|.|.|..||.....||.|.|.|")); System.out.println(BarCode.translate(12561)); // too short System.out.println(BarCode.translate("|.||..|.|.|.|..||.....||.|.|.|")); // too long System.out.println(BarCode.translate("|..||..|.|.|.|..||.....||.|.|.|")); // mssing frame bar System.out.println(BarCode.translate("....||..|.|.|.|..||.....||.|.|.|")); // wrong check digit System.out.println(BarCode.translate("|...||..|.|.|.|..||.....||...|||")); // invalid character System.out.println(BarCode.translate("|...||..|.|.|.|..||.x...||.|.|.|")); } }