Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For faster sorting of letters, the United States Postal Service encourages compa

ID: 3673506 • Letter: F

Question

For faster sorting of letters, the United States Postal Service encourages companies that use large volumes of mail to use a bar code denoting the ZIP. These 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 9, 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 following table: where 0 denotes a half bar and a 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 expression is 0, which would yield 11 according to the weights formula. Write a program that asks the user for a ZIP code and prints the bar code. Use : for half bars, | for full bars. For example, 05014 becomes: (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 digits. What to submit Submit your work via Blackboard's assignment tool. You should submit a zip folder with your java project. Your project must be well documented. Make sure to run the java doc program to obtain a prettily formatted version of your documentation in HTML format.

Explanation / Answer

import java.util.Scanner; public class Bars { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a zip code: "); int zipcode = input.nextInt(); input.close(); int checkDigitone = to_checkDigit(zipcode); String barcode = "|"; barcode = convert_digitToBarCode(checkDigitone) + barcode; for (int i = 0; i < 5; i++) { barcode = convert_digitToBarCode(checkDigitone) + barcode; zip /= 10; } barcode = "|" + barcode; System.out.println(barcode); } public static int to_checkDigit(int zipcode) { int remainder = zipcode; int sum_here = 0; while (remainder > 0) { sum_here += remainder % 10; remainder /= 10; } return 10 - (sum_here % 10); } public static String convert_digitToBarCode(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 "||:::"; } }