Assignments will gradually begin to involve larger and larger programs, made up of multiple classes. Think carefully about the design of your program, its classes and methods. Also, do not forget to keep on commenting your code and using good method decomposition and design in your homeworks.
Have you ever seen the front of a postmarked letter that has short and long dashes in a barcode style pattern? (See the picture below.) That is actually a machine readable encoding of a zip code that helps the US Postal Service sort and deliver bulk mail.
Bar codes use short and long vertical bars in the representation. Because we do not have printable characters for both symbols, we will represent long bars (ones) with the vertical bar character "|" and short bars (zero) with the colon ":".
Here is how a zip code is represented with these bars: Each code has a long bar "|" on both ends. In between those, there is a sequence of 6 sets of bars, where each of the first 5 sets corresponds to one of the digits in a zip code. The final set is a check digit that makes the sum of the zip code numbers and check digit equal to a multiple of 10. Thus, zip code 95014 needs need a check digit of 1 ( 9+5+0+1+4=19 + 1 = 20).
Each set of numbers has 5 digits, where each digit represents a different value to be added together, much like the digits in a binary number representation. For the bar codes here, however, the values are slightly different. From least significant to most significant (right-to-left), the digits represent the values 0, 1, 2, 4, and 7. A unique, predefined pattern of 0's and 1's (short bars and long bars) represents each number in a zip code. Numbers are encoded as follows:
| 7 | 4 | 2 | 1 | 0 | Code | |
| 1 | 0 | 0 | 0 | 1 | 1 | :::|| |
| 2 | 0 | 0 | 1 | 0 | 1 | ::|:| |
| 3 | 0 | 0 | 1 | 1 | 0 | ::||: |
| 4 | 0 | 1 | 0 | 0 | 1 | :|::| |
| 5 | 0 | 1 | 0 | 1 | 0 | :|:|: |
| 6 | 0 | 1 | 1 | 0 | 0 | :||:: |
| 7 | 1 | 0 | 0 | 0 | 1 | |:::| |
| 8 | 1 | 0 | 0 | 1 | 0 | |::|: |
| 9 | 1 | 0 | 1 | 0 | 0 | |:|:: |
| 0 | 1 | 1 | 0 | 0 | 0 | ||::: |
The digit is computed from the barcode by using the column number as follows:
:::|| = 0*7 + 0*4 + 0*2 + 1*1 + 1*0 = 1
|:|:: = 1*7 + 0*4 +1*2 + 0*1 + 0*0 = 9
The only exception is zero which yields the number 11 (1*7 + 1*4 + 0*2 + 0*1 + 0*0 = 11)
Write a GUI application that can take in a numeric zip code and output a barcode, or take in a barcode and output the numeric zip code. You should display an error message dialog if the barcode is not valid, which could occur because of an incorrect digit code (i.e. |||||) or a check digit that does not sum to 20. Additionally, all numeric zip codes are valid except those beginning with two consecutive zeroes (i.e. 00765 is not a valid zip, but 01765 is).
For example, if the user enters 95014, the program should produce: ||:|:::|:|:||::::::||:|::|:::||| or vice versa. (Don't forget to strip off the | at the first and | at the end before decoding).
You should have a Digit class, a BarCode class and a Convertor class. The Convertor class should have a main method that uses dialog boxes to obtain the input and then calls the other classes for help as needed. Your output should be shown in a message dialog. Your program should work in the following way: Prompt the user to enter a zip code and then display the resulting bar code. Next, prompt for the user to enter a bar code and then you display the zip code. Next, ask the user if s/he wishes to continue. If so, go back and repeat the above set of two operations and the continuation question.
Each of the Digit and BarCode classes should include a method "public String convert()". The Digit's convert method returns a string of "|" and ":" characters. The BarCode's convert method returns a string with an integer inside.
Also, you must provide the constructors:
public Digit(String s); // e.g., "17837"
public BarCode(String s); // e.g., "||:|:: ...
that instantiate an object of each classes, respectively.
After you have finished your program, turn the files in via Webwork You will be submitting multiple files. Please make sure they are named as shown below: