University of Ottawa
    School of Information Technology and Engineering

    CSI1102: Lab Assignment #2

    Due to be marked in your scheduled lab time during the week of February 14, 2005.
    1. Classes [20 marks]

      A Universal Product Code (UPC) is a machine-readable code shown on almost every product you purchase from any store (e.g., CDs, Coke, etc.) This code uniquely identifies the product so that no two products may have the same code. It was invented to speed up the process at the cash register and keep track of inventory. A UPC symbol usually contains two parts:
      1. Several bars that can be read by a scanner (bar code).
      2. The number representing the UPC. This number is the representation of the bars; i.e., there is one-to-one relationship between how the bars look like and the number or the code.

      The code is a 12-digit number consisting of three parts:

      1. 6-digit manufacturer identification number
      2. 5-digit item number
      3. The last digit is a check digit

      A manufacturer should have a unique 6-digit manufacturer identification number (the first component). A manufacturer can produce many items and each of these items can have its own UPC. This is done by assigning a 5-digit number that uniquely identifies the product within the manufacturer's production line (the second component). Thus, an 11-digit number should identify the product; but what about the 12th digit? This 12th digit is a check digit. It is used during scanning to make sure that the code read is the right code. If the digit calculated is different from the one scanned, then there is a problem and a re-scanning will be required.

      How can we calculate the check digit? Consider the UPC shown on the back of a pack of facial tissues. This code is 681131 59592 6. We want to know how the digit 6 is calculated. The steps are the following:

      • Step 1: Add the values of the digits in odd positions (6+1+3+5+5+2 = 22)
      • Step 2: Multiply the number you got by 3 (22*3 = 66)
      • Step 3: Add the values of the digits in even positions (8+1+1+9+9 = 28)
      • Step 4: Add the values of Step 2 and Step 3 (66+28 = 94)
      • Step 5: The check digit is the digit that when added to the above number produces a multiple of 10 (94 + 6 = 100, so 6 is the check digit.)

      Note that: The book industry uses the European Article Number (EAN). This code is a 13-digit code starting with 978 followed by the first left 9 digits of the ISBN and a check digit. Have a look at the back of your textbook! (The little bar code to the right should indicate the currency and the price. 90000 indicates that there are no data encoded for the price!) You are not asked about this type of bar code in this question.

      How can we draw the bars? The bars scanned vary in width. Every digit of the 12 digits is represented by a series of bars and spaces. Consider the following image, which contains the bars for the above UPC (681131 59592 6).

      Suppose that the thinnest bar or space represents a unit width. Relating to this unit width, every bar or space is represented by 1, 2, 3, or 4 units. The start of any UPC is 1-unit-wide-bar, then 1-unit-wide-space followed by 1-unit-wide-bar. (For simplicity, we will call this [1-bar, 1-space, 1-bar].) The digits to follow in the UPC are represented using the following table:

           0 = 3-2-1-1
           1 = 2-2-2-1
           2 = 2-1-2-2
           3 = 1-4-1-1
           4 = 1-1-3-2
           5 = 1-2-3-1
           6 = 1-1-1-4
           7 = 1-3-1-2
           8 = 1-2-1-3
           9 = 3-1-1-2
      
      With this table as a guide, we should know how the above UPC is represented. For example, the first two digits of the manufacturer identification part (i.e., 6 and 8) are represented as [1-space, 1-bar, 1-space, 4-bar] and [1-space, 2-bar, 1-space, 3-bar]. The following 4 digits are represented the same way. Right after the end of the 6th digit (the middle), we have [1-space, 1-bar, 1-space, 1-bar, 1-space]. Then the rest of the digits are optically inverted. This means that we can still use the above table but we start with a bar instead of a space. For example, 5 is represented as [1-bar, 2-space, 3-bar, 1-space]. The UPC ends with [1-bar, 1-space, 1-bar] as the start.

      Using the above information and the object-oriented concept of classes, deliver the following two requirements. You can include both requirements in one program that contains different files for different classes. Partial marks will be allocated for partial steps.



      • (1.1) Design and Implement an application that produces a UPC. The 3 components are obtained as follows:
        1. The application should ask for your 7-digit student number and produce a manufacturer identification number by excluding the least significant digit and reverse the other 6 digits. If your student number is less than 7-digit number, add zeros to the left to get a 7-digit number. For example, suppose that your student number is 12345. Then, you add zeros to the left to get 0012345. Exclude the least significant digit to get 001234. Now, reverse that number to get 432100. This is your manufacturer identification number.
        2. Use a random number generator to generate the 5-digit item number.
        3. Calculate the check digit as explained above.



      • (1.2) Extend your application to draw the bars representing your UPC.

        Hint: In order to create a window for painting, you may use the following code in the main method:

            // Create a window to draw UPC code      
            WindowUPC myWindow = new WindowUPC(..);
            myWindow.setSize(105, 105);
            myWindow.show();
                
            // You should ask the user to press some button. 
            // When accepted, you close the window
            // ...
            myWindow.exitWindow();
        
        and similar to the definition of applets we saw in class, you can define WindowUPC as:
            import java.awt.*;
            
            public class WindowUPC extends Frame{
            
                ...
                
                public WindowUPC(..){
                    ...
                }
                
                public void paint (Graphics page){
                    ...
                }
                    
                public void exitWindow (){
                    System.exit(0); 
                }
                ...
            }
        



      Notes
      • You must use objects and classes to represent a UPC.
      • Pay attention that a student number 12345 should result in manufacturer identification number 432100 not 4321.
      • You must print the generated 12-digit UPC as a result for 1.1.
      • ... indicate that there is some code missing.
      • Example:
        
        Enter your student number: 0012345
        The UPC: 432100 83120 6
        
        Enter 0 to exit:
        


    2. Simple Applet [10 marks]

      Design and implement a Java Applet Colors.java that displays a color palette using the following sequence: red, yellow, green, cyan, blue, magenta and ending with red again. RGB components should be handled gradually going from one color to another. Refer to Figure 2.21 of the textbook for the RGB components for each of the above colors.

      Create the Java bytecode and embed it in an HTML document using the applet tag to show the color palette. The applet size should be 768 × 50 pixels. You should have a palette like this:

      Hint: In order to create a color, you may use the following constructor:

          Color c = new Color(R, G, B);
      
      where R, G and B are the specified red, green and blue values. The Color class is defined in the package java.awt