INTRODUCTION TO COMPUTER SCIENCE
Robert Sedgewick and Kevin Wayne


This is the syntax highlighted version of ZipBarCoder.java.

/*************************************************************************
 *  Compilation:  javac ZipBarCoder.java
 *  Execution:    java ZipBarCoder N
 *  
 *  Reads in a 5 digit zip code and prints out the postal barcode.
 *
 *************************************************************************/

public class ZipBarCoder {

   public static void main(String[] args) { 
      int N = Integer.parseInt(args[0]);
      int[] digits = new int[6];
      int[][] code = { { 1, 1, 0, 0, 0 },
                       { 0, 0, 0, 1, 1 },
                       { 0, 0, 1, 0, 1 },
                       { 0, 0, 1, 1, 0 },
                       { 0, 1, 0, 0, 1 },
                       { 0, 1, 0, 1, 0 },
                       { 0, 1, 1, 0, 0 },
                       { 1, 0, 0, 0, 1 },
                       { 1, 0, 0, 1, 0 },
                       { 1, 0, 1, 0, 0 } };

      // extract digits
      for (int i = 1; i <= 5; i++) {
         digits[i] = N % 10;
         N /= 10;
      }

      // compute check digit
      int checkdigit = 0;
      for (int i = 1; i <= 5; i++)
         checkdigit += digits[i];
      digits[0] = checkdigit % 10;

      // print barcode
      for (int i = 5; i >= 0; i--)
         for (int j = 0; j < 5; j++)
            if (code[digits[i]][j] == 1) System.out.println("*****");
            else                         System.out.println("**");
   }
}


Last updated: Wed Feb 11 18:16:34 EST 2004 .
Copyright © 2004, Robert Sedgewick and Kevin Wayne.