Question

In: Computer Science

In java For faster sorting of letters, the U.S. Postal Service encourages companies that send large...

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

Solutions

Expert Solution

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


Related Solutions

Consider a project to supply 70 million postage stamps to the U.S. Postal Service for the...
Consider a project to supply 70 million postage stamps to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $279,000 five years ago; if the land were sold today, it would net you $130,000, aftertax. You estimate the land can be sold for $400,000 after taxes in five years. You will need to install $1,867,000 in new manufacturing plant and equipment to actually produce the stamps; this plant and equipment...
Consider a project to supply 98 million postage stamps per year to the U.S. Postal Service...
Consider a project to supply 98 million postage stamps per year to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $1,695,000 five years ago; if the land were sold today, it would net you $1,770,000 aftertax. The land can be sold for $1,746,000 after taxes in five years. You will need to install $5.25 million in new manufacturing plant and equipment to actually produce the stamps; this plant and...
Consider a project to supply 98 million postage stamps per year to the U.S. Postal Service...
Consider a project to supply 98 million postage stamps per year to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $1,695,000 five years ago; if the land were sold today, it would net you $1,770,000 aftertax. The land can be sold for $1,746,000 after taxes in five years. You will need to install $5.25 million in new manufacturing plant and equipment to actually produce the stamps; this plant and...
Consider a project to supply 90 million postage stamps per year to the U.S. Postal Service...
Consider a project to supply 90 million postage stamps per year to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $1,655,000 five years ago; if the land were sold today, it would net you $1,730,000 aftertax. The land can be sold for $1,738,000 after taxes in five years. You will need to install $4.85 million in new manufacturing plant and equipment to actually produce the stamps; this plant and...
Consider a project to supply 92 million postage stamps per year to the U.S. Postal Service...
Consider a project to supply 92 million postage stamps per year to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $1,665,000 five years ago; if the land were sold today, it would net you $1,740,000 aftertax. The land can be sold for $1,740,000 after taxes in five years. You will need to install $4.95 million in new manufacturing plant and equipment to actually produce the stamps; this plant and...
Consider a project to supply 102 million postage stamps per year to the U.S. Postal Service...
Consider a project to supply 102 million postage stamps per year to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $1,715,000 five years ago; if the land were sold today, it would net you $1,790,000 aftertax. The land can be sold for $1,750,000 after taxes in five years. You will need to install $5.45 million in new manufacturing plant and equipment to actually produce the stamps; this plant and...
Consider a project to supply 60 million postage stamps per year to the U.S. Postal Service...
Consider a project to supply 60 million postage stamps per year to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $760,000 five years ago; if the land were sold today, it would give you $912,000 after taxes. Three years ago, you purchased some equipment for $60,000. This equipment has a current book value of $40,000 and a current market value of $30,000. The land and equipment can be used...
Consider a project to supply 104 million postage stamps per year to the U.S. Postal Service...
Consider a project to supply 104 million postage stamps per year to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $1,725,000 five years ago; if the land were sold today, it would net you $1,800,000 aftertax. The land can be sold for $1,752,000 after taxes in five years. You will need to install $5.55 million in new manufacturing plant and equipment to actually produce the stamps; this plant and...
Consider a project to supply 97 million postage stamps per year to the U.S. Postal Service...
Consider a project to supply 97 million postage stamps per year to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $1,690,000 five years ago; if the land were sold today, it would net you $1,765,000 aftertax. The land can be sold for $1,745,000 after taxes in five years. You will need to install $5.2 million in new manufacturing plant and equipment to actually produce the stamps; this plant and...
Consider a project to supply 60 million postage stamps per year to the U.S. Postal Service...
Consider a project to supply 60 million postage stamps per year to the U.S. Postal Service for the next five years. You have an idle parcel of land available that cost $760,000 five years ago; if the land were sold today, it would give you $912,000 after taxes. Three years ago, you purchased some equipment for $60,000. This equipment has a current book value of $40,000 and a current market value of $30,000. The land and equipment can be used...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT