California State University, Dominguez Hills

Department of Computer Science

Spring 2005

CSC121A Lab 09:  Designing Classes

 

Objectives:

1.      To practice designing multiple classes in a program

2.      To understand the cohesion and coupling concepts

3.      To familiarize the relationships between classes

4.      To be able to identify the functions of each class

 

Exercises P7.11 on Page 327: Postal bar codes

For faster sorting of letters, the United States Postal Service encourages companies that send large amount volumes of mail to use a bar code denoting the ZIP code (see the figure below).

 

A Postal Bar Code

******************* ECRLOT ** CO57

 

CODE C671RTS2

JOHN DOE                                               CO57

1009 FRANKLIN BLVD

SUNNYALE                     CA 95014-5143

 

||||||||||||||||||||||||||||||||||||||||||||||||||

 

 

The encoding scheme for a five-digit ZIP code is shown in the following figure.

 

Encoding for Five-Digit Bar Codes

                                          Frame bars

                              ||||||     |||||     |||||     |||||    |||||    ||||||

 

There are full-height frame bars on each side. The five encoded digits are followed by a correction digit, which is computed as follows: Add up all digits, and choose the correction digit to make the sum a multiple of 10. For example, the ZIP code 95014 has sum of digits 19, so the correction digit is 1 to make the sum equal to 20.

 

Each digit of the ZIP code, and the correction digit, is encoded according to the following table:

 

 

7

4

2

1

0

1

0

0

0

1

1

2

0

0

1

0

1

3

0

0

1

1

0

4

0

1

0

0

1

5

0

1

0

1

0

6

0

1

1

0

0

7

1

0

0

0

1

8

1

0

0

1

0

9

1

0

1

0

0

0

1

1

0

0

0

 

where 0 denotes a half bar and 1 a full bar. Note that they represent all combinations of two full and three half bars. The digit can be easily computed 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 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, 90514 becomes

||:|:::|:|:||::::::||:|::|:::|||

Use classes BarCode and Digit in your solution.

 

Requirements:

 

1.      Create a project named CSC121A-Lab09 in BlueJ.

2.      Create a class BarCodeTest under this project, and type the source code that contains the main method in it. Your main method should read a ZIP code, create an object of BarCode with the input ZIP code (see 3), call the method getBarCode() (as below) to return the bar code for the given ZIP code and display on the screen.

3.      Create a class BarCode under the project, and type the source code as described in Exercise P7.11. Your BarCode should have a constructor that accepts a ZIP code, and provides a method called getBarCode() that converts the input ZIP code to a bar code by using class Digit (see below).

4.      Create a class Digit under the project, and type the source code. Your Digit class should have a method to encode the given digit described in Exercise 7.11.

5.      Compile all source code and run the byte code with the command.

6.      Your source code documentation must follow the instructions of Chapter 2.8, pages 54-56. Be advised to briefly comment all classes, constructors, and methods.

 

Class Design

1.      BarCode

a.       Instance field:    A single field String code for the input ZIP code

b.      Constructor:     Initialize code to the given ZIP code in the parameter

c.       Method:           A single method getBarCode() to convert code to  the bar code

2.      Digit

a.       Instance field: none

b.      Constructor:     none

c.       Method:           A static method getCode(int i) to convert a digit to the bar code according to the weights table

3.      BarCodeTest: The main method accepts a ZIP code from the user, creates an object of BarCode, the call the method getBarCode() of the object, and prints out the result.

 

Implementation

/**

 * A Class to generate a bar code for a given ZIP code

 * @author <…>

 * @date <…>

 */

 

public class BarCode {

 

      /**

       * Fields

       */

      private String code;

 

      /**

       * Constructor — initialize the instance field

       * @param String pc the given ZIP code

       */

      public BarCode(String pc) {

            code = pc;

      }

                 

      /**

       * Convert postal code to bar code

       * @return String the bar code

       */

      public String getBarCode() {

            String ret = "|";

            // To be completed

           // Use loop structure

           //   take out each digit from the ZIP code

           //   convert each digit to the bar code

           //   concatenate the bar codes to ret

           //   find the sum of all digits

           // Find the correction digit and convert to bar

           // code and concatenate to ret

             

            return ret + "|";

      }

}

 

/**

 * A Class to convert single digits to bar code

 * @author <…>

 * @date <…>

 */

public class Digit {

     

      /**

       * Convert digit to bar code

       * @param int i a digit to be converted to bar code

       */

      public static String getCode(int i) {

            // To be completed

            // use multi-decision (nested if or switch) to

            // convert the digit to bar code according to

            // the weights table

      }

}

/**

 * A test Class to test postal-bar code conversion

 * @author <…>

 * @date <…>

 */

public class Digit {

      public final static void main(String[] args) {

            // To be completed

            // Use input dialog to accept a ZIP code

            // Use the ZIP code to create an object of BarCode

            // Call the method getBarCode of the object to get

            //    the bar code

            // Display the bar code

      }

}

 

Exercise

Complete the “To be completed” parts above.