The US postal barcode is used by the US Postal System to route mail. Each decima
ID: 3938353 • Letter: T
Question
The US postal barcode is used by the US Postal System to route mail. Each decimal digit in the zip code is encoded using a sequence of 5 short and long lines for use by scanners as follows: 0: | | s s s (|: long, s: short) 1: s s s || 2: s s | s | 3: s s | | s 4: s | s s | 5: s | s | s 6: s | | s s 7: | s s s | 8: | s s | s 9: | s | s s A sixth checksum digit is appended: it is computed by summing up the original five digits mod 10. In addition, a long line is added to the beginning and appended to the end. Write a program that reads in a five digit zip code and prints the corresponding postal barcode. Print the code horizontally. e g, the following encodes 13210 (with the check digit of 7). 13210 zip code's postal barcode is^^^^^^^^rightarrow This is not output 1 3 2 1 0 7 rightarrow This is not outputExplanation / Answer
import java.util.Scanner;
public class Barcode {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String codeStr = scan.nextLine(); //scanning code from user
scan.close();
//data for conversion
String data[] = {
"llsss",
"sssll",
"sslsl",
"sslls",
"slssl",
"slsls",
"sllss",
"lsssl",
"lssls",
"lslss"
};
int code[] = new int[6];
code[0]= codeStr.charAt(0) - '0'; //converting character to int
code[1]= codeStr.charAt(1) - '0';
code[2]= codeStr.charAt(2) - '0';
code[3]= codeStr.charAt(3) - '0';
code[4]= codeStr.charAt(4) - '0';
code[5]= (code[0] + code[1] + code[2] + code[3] + code[4])%10; //calculate checksum
String upper="*";//add star at start
for(int i=0;i<6;i++){
for(int j=0;j<5;j++)
{
if(data[code[i]].charAt(j) == 'l'){
upper += "*";
}
else
upper +=" ";
}
}
upper += "*"; //add star at end
//calculate how long string will be
int runtime = code.length * data[0].length() + 2;
//print upper stars
for(int j=0;j<2;j++){
for(int i=0;i<runtime;i++){
System.out.print(upper.charAt(i));
}
System.out.println();
}
//print lower starts
for(int j=0;j<3;j++){
for(int i=0;i<runtime;i++){
System.out.print("*");
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.