Please use java.util.Scanner !!!! Business P5.25 Postal bar codes. For faster so
ID: 3744242 • Letter: P
Question
Please use java.util.Scanner !!!!
Business P5.25 Postal bar codes. For faster sorting of letters, the United States Postal Service encour- ages companies that send large volumes of mail to use a bar code denoting the zip code (see Figure 6). 42 Chapter 5 Methods CODE C671RTS2 JOHN DOE 1009 FRANKLIN BLVD SUNNYVALE CA 95014-5143 C057 Figure 6 A Postal Bar Code The encodine scheme for a five-digit zip code is shown in Figure 7-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 ex the check digit is 1 to make the sum equal to 20. the zip code 95014 has a sum of 19, so Frame bars Digit 1 Digit 2 Digit 3 Digit4 Digit 5 Check Figure 7 Encoding for Five-Digit Bar Codes Each digit of the zip code, and the check digit, is encoded according to the table below, where 1 denotes a full bar and O a half bar: Bar 1 (weight 7) Bar 2 (weight 4) Bar 3 (weight 2) Bar 4 (weight1) Bar 5 (weight 0) Digit The digit can be easily computed from the bar code using the column weights 7, 4,2, 1,0. For example, 01 100 is Ox7+1x4+1x2+0x1+0x0=6. "The only exception is 0, which would yield 11 according to the weight formula. Programming Exercises 243 Write a p half bars, I for full bars. For example, 95014 becomes that asks the user for a zip code and prints the bar code. Use:for Provide these methods: public static void printdigitCint d) public static void printBarCodeCint zipCode)Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Barcode.java
//Postnet.java
import java.util.Scanner;
public class Barcode {
// constants to represent characters for full bar and half bar
public static final char FB = '|', HB = ':';
// an array of Strings to represent the barcodes of digits from 0 to 9
//(based on the given table)
private static String[] barcode = { "" + FB + FB + HB + HB + HB,
"" + HB + HB + HB + FB + FB, "" + HB + HB + FB + HB + FB,
"" + HB + HB + FB + FB + HB, "" + HB + FB + HB + HB + FB,
"" + HB + FB + HB + FB + HB, "" + HB + FB + FB + HB + HB,
"" + FB + HB + HB + HB + FB, "" + FB + HB + HB + FB + HB,
"" + FB + HB + FB + HB + HB };
/**
* method to print the bar code of a zipcode
* @param zipCode- integer zip code
*/
public static void printBarCode(int zipCode) {
// printing start full frame bar
System.out.print(FB);
int sum = 0; // to store the sum of digits
// converting to string, for ease of calculation
String digits = String.valueOf(zipCode);
// looping through all digits in the zipcode
for (int i = 0; i < digits.length(); i++) {
// parsing the digit
int digit = Integer.parseInt("" + digits.charAt(i));
// adding to total
sum += digit;
// printing barcode of digit
printDigit(digit);
}
// finding remainder of sum when divided by 10
int remainder = sum % 10;
if (remainder == 0) {
// check digit is 0
printDigit(0);
} else {
// check digit is 10-remainder
printDigit(10 - remainder);
}
// printing stop full frame bar
System.out.print(FB + " ");
}
/**
* method to print the bar code of a digit between 0 and 9
*/
public static void printDigit(int d) {
if (d >= 0 && d <= 9) {
System.out.print(barcode[d]);
}
}
public static void main(String[] args) {
//scanner to read user input
Scanner sc = new Scanner(System.in);
//promting and getting zip code
System.out.print("Enter a zip code: ");
String input = sc.nextLine();
try {
int zip = Integer.parseInt(input);
// displaying the barcode
printBarCode(zip);
} catch (Exception e) {
// invalid number
System.out.println("Invalid input");
}
}
}
/*OUTPUT*/
Enter a zip code: 95014
||:|:::|:|:||::::::||:|::|:::|||