In: Computer Science
In java
For faster sorting of letters, the U.S. Postal Service encourages companies that send large volumes of mail to use a bar code denoting the ZIP code (see below).
The encoding scheme for a five-digit ZIP code is shown below There are full-height frame bars on each side. The five encoded digits are followed by a check digit, which is computed as follows: Add up all digits, and choose the check digit to make the sum a multiple of 10. For example, the sum of the digits in the ZIP code 95014 is 19, so the check digit is 1 to make the sum equal to 20.
Each digit of the ZIP code, and the check digit, is encoded according to the table below, where 0 denotes a half bar and 1 a full bar. Note that they represent all combinations of two full and three half bars. The digit can be computed easily from the bar code using the column weights 7, 4, 2, 1, 0. For example, 01100 is
0 × 7 + 1 × 4 + 1 × 2 + 0 × 1 + 0 × 0 = 6
The only exception is 0, which would yield 11 according to the
weight formula - to represent the barcode , you can use the table
below also.
Weight Digit 74210
1. 00011
2. 00101
3. 00110
4. 01001
5. 01010
6. 01100
7. 10001
8. 10010
9. 10100
0. 11000
Write a program that asks the user (a) for a ZIP code and prints the bar code. (b) for a barcode and prints the ZIP code. Use classes to implement this solution.
Use : for half bars, | for full bars. For example, 95014 becomes
||:|:::|:|:||::::::||:|::|:::|||
You can learn more about zip code encoding here: https://en.wikipedia.org/wiki/POSTNET
I am not converting bar code to zip as the question is already very long But i hope you can do it by yourself as the logic is just reverse of the one shown below
Below is the JAVA code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
import java.util.*;
class ZipToBar
{
public static int getZipCode()
{
System.out.print("Enter a ZIP code: ");
Scanner sc = new Scanner(System.in);
int zip = sc.nextInt();
return zip;
}
public static int checkDigit(int zip)
{
int temp = zip;
int sum = 0;
while (temp > 0)
{
sum = sum+(temp)%10;
temp = temp/10;
}
return 10 - (sum % 10);
}
public static String getBarCode(int digit)
{
if (digit == 1)
{
return ":::||";
}
if (digit == 2) {
return "::|:|";
}
if (digit == 3) {
return "::||:";
}
if (digit == 4) {
return ":|::|";
}
if (digit == 5) {
return ":|:|:";
}
if (digit == 6) {
return ":||::";
}
if (digit == 7) {
return "|:::|";
}
if (digit == 8) {
return "|::|:";
}
if (digit == 9) {
return "|:|::";
}
return "||:::";
}
}
public class test
{
public static void main(String []args)
{
ZipToBar z = new ZipToBar();
int zipCode = z.getZipCode(); //get user input
int checkDigit = z.checkDigit(zipCode); //find check digit
int temp = zipCode;
String zipString = Integer.toString(zipCode); //convert from
integer to String
char ch;
System.out.print("|"); //between frame
for(int i=0;i<5;i++)
{
ch = zipString.charAt(i); //traverse each character
System.out.print(z.getBarCode(Character.getNumericValue(ch)));
//print zip code for each character
}
System.out.print("|"); //between frame
}
}
Below is the screenshot of output
I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any