Zip code class This is a test class for your zip code c

//********************************* // // Zip code class // // This is a test class for your zip code class. // Your Zipcode class must work with this test class. // Do not alter this 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 code = new Zipcode(zip);

System.out.println(\"The bar code: \");         

System.out.println(code.getBarcode());    

}

}  

P8.3 For faster sorting of letters, the United States Postal Service encourages companies that send large volumes of mail to use a bar code denoting the ZIP code (see igure The encoding scheme for a five-digit ZIP code is shown in Figure 8. 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 at right, where 0 denotes a half bar and 1 a full bar. Note that they represent all combina- tions 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 Digit 7 42 e only exception is 0, which would yield 11 according to the weight 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, 95014 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.

Solution

Zipcode.java

public class Zipcode
{
public static int checkDigit = checkDigit(zip);

        String barcode = \"|\";
        barcode = digitToBarCode(checkDigit) + barcode;
        for (int i = 0; i < 5; i++) {
            barcode = digitToBarCode(checkDigit) + barcode;
            zip /= 10;
        }
        barcode = \"|\" + barcode;
        System.out.println(barcode);
    }

    public static int checkDigit(int zip) {
        int remaining = zip;
        int sum = 0;
        while (remaining > 0) {
            sum += remaining % 10;
            remaining /= 10;
        }
        return 10 - (sum % 10);
    }

    public static String 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 \"||:::\";
    }
}
}

//********************************* // // Zip code class // // This is a test class for your zip code class. // Your Zipcode class must work with this test clas
//********************************* // // Zip code class // // This is a test class for your zip code class. // Your Zipcode class must work with this test clas

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site