The code is a 12-digit number consisting of three parts:
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:
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-2With 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.
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); } ... }
Enter your student number: 0012345 The UPC: 432100 83120 6Enter 0 to exit:
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