The post office uses a scheme for converting an ordinary zip code, such as 08904, into a bar code. In this program you do the conversion. For example, here is the box that pops up when my program starts:
After the program does its calculations, this box pops up with the result:
Here is how the post office's conversion scheme works. Each digit of a zip code is converted into a 5-digit binary number. For example, a '1' in a zip code is always converted into '00011'. Then the binary number is converted into a bar code. The 0's in the number are displayed as short bars (:), and the 1's as tall bars (|). The bar code at this point has 5 x 5 = 25 bars. An example is shown on p. 327 in the text.
A 6th digit, called a 'correction digit', is also displayed in the bar code. To find the correction digit, first add together the 5 digits of the ordinary zip code. For 08904, the sum is 0 + 8 + 9 + 0 + 4 = 21. The correction digit is the number you'd have to add to this sum to reach a multiple of 10. The nearest multiple of 10 is 30, so the correction digit here is 9. This 9 would be converted to a 5-digit binary number, and then to short and tall bars, and would be displayed as the 6th digit in the bar code. The bar code at this point has 6 x 5 = 30 bars.
Finally, to complete the bar code, 1 frame bar (|) is added both before and after the number. The final bar code has 32 bars.
Here is the table that the post office uses to convert the digits 0 to 9 into 5-digit binary numbers:
Use classes Digit, BarCode and BarCodeVisualizer in your solution. In each class, write methods to do specific tasks. Here is how I broke down the various tasks (but you can set it up differently):
This class handles tasks related to converting a 5-digit zip code into a string of 0's and 1's.
This class converts a string of 0's and 1's into a bar code.
This class handles the visual aspects of the bar code -- how does the bar code look when it is displayed? It is generally a good idea in object-oriented design to separate the visual, display aspects of a program from its other operations, and this class does that here.
You can put 'main' either in one of these 3 classes or in a separate 4th class called 'BarCodeGenerator', which has only 'main' in it. (I wrote a separate class 'myBarCodeGenerator.java' to hold 'main()'.) In 'main', ask the user for a zip code. Create a Digit object, and send the zip code to the Digit object for processing. Create a BarCode object, take the result from the Digit object, and send it to the BarCode object for further work. Finally, display the bar code that the BarCode object returns to 'main'.
Although this program deals with numbers, I found it convenient to use mostly Strings in storing and working with the zip codes. Use the 'substring()' method of the String class to extract small strings from larger strings. For example, 'myString.substring(i, i+1)' gives you the 'ith' character in 'myString'.
Use 'JOptionPane.showInputDialog()' to get input and 'JOptionPane.showMessageDialog(null, String x)' to display output. Import 'javax.swing.JOptionPane'.
You can download BarCode.zip which contains the 4 interacting classes that you need to write for this program.
This programming assignment is based directly on Exercise P7.11 in Computing Concepts with Java Essentials, Third Edition, by Cay Horstmann.
To see how the program runs when it is done, you can download CompletedBarCodes.jar. Depending on which browser you are using, the program may run if you choose 'Open'. If not, you will have to select 'Save'. After the file downloads, double-click it and the completed program should run. (The file extension may change from .jar to .zip during the download. If this occurs, either while downloading or after downloading rename the file and change the file extension back to .jar.) (Thanks to Michael DeRupo for his help with JAR files.)