P8.6 For faster sorting of letters, the U.S. Postal Service encourages companies
ID: 3592410 • Letter: P
Question
P8.6 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 Figure 8) The encoding scheme for a five-digit ZIP code is shown in Figure 9. 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 ECRLOT CO57 CODE C671RTS2 JOHN DOE 1009 FRANKLIN BLVD SUNNYVALE Frame bars CO57 CA 95014 5143 iDit Digit 1 Digit 2 Digit 3 Digit 4 Digit 5 Check Figure 8 A Postal Bar Code Figure 9 Encoding for Five-Digit Bar Codes ter 8 Designing Classes 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 at right, where 0 denotes a half bar and 1 a full bar. Note that thev 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 Weight 7 4 2 10 0 0 0 1 1 Digit 0x7+1x4+1x2+0x1+0x0-6 4 The only exception is 0, which would yield 11 according to the weight formula. Write a program that asks the user for aZIP code and prints the bar code. Use: for half bars, | for full bars. For example, 95014 becones 1 010 0 (Alternatively, write a graphical application that draws real bars.) Your program should also be able to carry out the opposite conversion: Translate bars into their ZIP code, reporting any errors in the input format or a mismatch of the digitsExplanation / Answer
Zipcode class:
import java.util.Scanner;
public class Zipcode {
public int num1, num2, num3, num4, num5, num6;// num1..num5 for each digit digit 1 to digit 5
public int checkDig; // check digit for the end
public static String bar0, bar1, bar2, bar3, bar4, bar5, bar6, bar7, bar8, bar9;
private int num;
private String barcode;
public Zipcode(int num) {
/*The barcodes according to the given table in the question*/
/*0->bar0 1->bar1 2->bar2 and so on..*/
this.num = num;
bar0 = "||:::";
bar1 = ":::||";
bar2 = "::|:|";
bar3 = "::||:";
bar4 = ":|::|";
bar5 = ":|:|:";
bar6 = ":||::";
bar7 = "|:::|";
bar8 = "|::|:";
bar9 = "|:|::";
}
public Zipcode(String barcode) {
// TODO Auto-generated constructor stub
this.barcode = barcode;
bar0 = "||:::";
bar1 = ":::||";
bar2 = "::|:|";
bar3 = "::||:";
bar4 = ":|::|";
bar5 = ":|:|:";
bar6 = ":||::";
bar7 = "|:::|";
bar8 = "|::|:";
bar9 = "|:|::";
}
public String getBarcode() {
/*Getting each digit from the input number*/
num1 = num / 10000;
num2 = ((num / 1000) - num1 * 10);
num3 = (num / 100 - (num1 * 100 + num2 * 10));
num4 = (num / 10 - (num1 * 1000 + num2 * 100 + num3 * 10));
num5 = (num - (num1 * 10000 + num2 * 1000 + num3 * 100 + num4 * 10));
checkDig = 100 - (num1 + num2 + num3 + num4 + num5);
while (checkDig > 10)
checkDig -= 10;//Getting check digit till > 10
/*Generating the barcode for each digit by encoding using getBar() method*/
String result = "|";
result += getBar(num1);
result += getBar(num2);
result += getBar(num3);
result += getBar(num4);
result += getBar(num5);
result += getBar(checkDig);
result += "|";
return result;
}
/*Method to Encode each number*/
public String getBar(int x) {
String result = "";
switch (x) {
case 0:
result = (bar0);
break;
case 1:
result = (bar1);
break;
case 2:
result = (bar2);
break;
case 3:
result = (bar3);
break;
case 4:
result = (bar4);
break;
case 5:
result = (bar5);
break;
case 6:
result = (bar6);
break;
case 7:
result = (bar7);
break;
case 8:
result = (bar8);
break;
case 9:
result = (bar9);
break;
}
return result;
}
/*Method to decode the bar number*/
public static int getNumber(String bar) {
int digit = 0;
if (bar.equals(bar0)) {
digit = 0;
}
if (bar.equals(bar1)) {
digit = 1;
}
if (bar.equals(bar2)) {
digit = 2;
}
if (bar.equals(bar3)) {
digit = 3;
}
if (bar.equals(bar4)) {
digit = 4;
}
if (bar.equals(bar5)) {
digit = 5;
}
if (bar.equals(bar6)) {
digit = 6;
}
if (bar.equals(bar7)) {
digit = 7;
}
if (bar.equals(bar8)) {
digit = 8;
}
if (bar.equals(bar9)) {
digit = 9;
}
return digit;
}
public int getZIPcode() {
String bar;
String num = "";
//The input barcode is divided into 5 blocks for each digit
try {
bar = barcode.substring(1, 6);
num += getNumber(bar);
bar = barcode.substring(6, 11);
num += getNumber(bar);
bar = barcode.substring(11, 16);
num += getNumber(bar);
bar = barcode.substring(16, 21);
num += getNumber(bar);
bar = barcode.substring(21, 26);
num += getNumber(bar);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("Invalid input for barcode try again!!");
}
return Integer.parseInt(num);//Decoded ZIP Code
}
}
ZipTest class
import java.util.Scanner;
public class ZipTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a ZIP code:");
int zip = input.nextInt();
Zipcode code1 = new Zipcode(zip);
System.out.println("The corresponding barcode :");
System.out.println(code1.getBarcode());
System.out.println("Now please provide a bar code:");
input.nextLine();
String bar = input.nextLine();
Zipcode code2 = new Zipcode(bar);
System.out.println("The corresponding barcode :");
System.out.println(code2.getZIPcode());
}
}
**********************************************************************************
Sample input output:
Please enter a ZIP code:
95014
The corresponding barcode :
||:|:::|:|:||::::::||:|::|:::|||
Now please provide a bar code:
||:|:::|:|:||::::::||:|::|:::|||
The corresponding barcode :
95014
***********************************************************************************
Note:
Try to avoid scanners for string input.
The code above works fine.
Kindly rate the answer.
Hope it helps.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.