Question
1
//********************************************************************
// UPC.java Author: R. Elias
//
// + Asks for student number and checks its
validity
// + Creates an instance of CodeUPC and prints
the code
// + Create an instance of WindowUPC to draw the
bar code
//********************************************************************
import
cs1.Keyboard;
public class
UPC{
public static void main (String[] args){
int studentID;
// ---------- Get the Student Number
System.out.print("Enter your student
number: ");
studentID = Keyboard.readInt();
// ---------- Check the validity
while (studentID < 0 || studentID >
9999999){
System.out.print("Invalid ID!
Enter a valid ID: ");
studentID = Keyboard.readInt();
}
// ---------- Create an instance of UPC
CodeUPC myCode = new CodeUPC (studentID);
System.out.println ("The UPC: "
+ myCode.getMID() + " " + myCode.getItemNumber() + " " +
myCode.getCheckDigit());
// ---------- Create a window to draw UPC
code
WindowUPC myWindow = new
WindowUPC(myCode);
myWindow.setSize(105, 105);
myWindow.show();
// ---------- Exit the window
int toExit;
do{
System.out.print("Enter 0 to exit:
");
toExit = Keyboard.readInt();
}while (toExit != 0);
myWindow.exitWindow();
}
}
//
------------------------------------------------------
//********************************************************************
// CodeUPC.java Author: R. Elias
//
// CodeUPC takes 3 integers and creates CodeUPC
objects
// that manipulates the UPC
//********************************************************************
import
java.util.Random;
public class
CodeUPC
{
// ---------- The three components in UPC
code
private int MID; // Manufacurer ID number
private
int itemNumber;
private int checkDigit;
//
------------------------------------------------------
public CodeUPC(int studentID){
// ---------- MID Part of UPC
MID = reverseNumber ((studentID -
studentID%10) / 10);
// ---------- itemNumber Number Part of
UPC
Random generator = new Random();
itemNumber = generator.nextInt(100000);
// ---------- Check Digit Part of UPC
calcCheckDigit();
}
//
------------------------------------------------------
// Calculates
the Check Digit in 5 Steps
//
------------------------------------------------------
private void calcCheckDigit (){
int step1, step2, step3, step4, step5;
step1 = MID/100000 +
(MID/1000 - MID/10000*10) +
(MID/10 - MID/100*10) +
itemNumber/10000 +
(itemNumber/100 -
itemNumber/1000*10) +
itemNumber%10 ;
step2 = step1 * 3;
step3 = (MID/10000
- MID/100000*10) +
(MID/100 - MID/1000*10) +
MID%10 +
(itemNumber/1000 - itemNumber/10000*10)
+
(itemNumber/10 -
itemNumber/100*10);
step4 = step2 + step3;
if (step4%10 != 0)
step5= 10 - step4%10;
else
step5 = 0;
checkDigit = step5;
}
//
------------------------------------------------------
// Reverses the
Order of Digits in Number
//
------------------------------------------------------
private int reverseNumber (int number){
int currentDigit, reverse = 0;
int copyNumber = number;
int
zeros = 0;
do{
currentDigit = copyNumber % 10;
reverse = (reverse * 10) +
currentDigit;
copyNumber = copyNumber / 10;
}
while (copyNumber > 0);
// Compute how many trailing zeros in
number
if (number > 99999) zeros = 0;
else if (number > 9999) zeros = 1;
else if (number > 999) zeros = 2;
else if (number > 99) zeros = 3;
else if (number > 9) zeros = 4;
else if (number > 0) zeros = 5;
if (zeros > 0)
reverse
*= Math.pow(10, zeros);
return reverse;
}
//
------------------------------------------------------
public int getMID(){
return MID;
}
//
------------------------------------------------------
public int getItemNumber(){
return itemNumber;
}
//
------------------------------------------------------
public int getCheckDigit(){
return checkDigit;
}
//
------------------------------------------------------
}
//
===================================================================
//********************************************************************
// WindowUPC.java Author: R. Elias
//
// WindowUPC takes CodeUPC object and draws the
bar code
//********************************************************************
import
java.awt.*;
public class
WindowUPC extends Frame{
private final static int startY = 40, endY =
90;
private static int loc = 10;
private static boolean black = true;
private int MID;
private
int itemNumber;
private int checkDigit;
public WindowUPC(CodeUPC myCode){
MID = myCode.getMID();
itemNumber = myCode.getItemNumber();
checkDigit = myCode.getCheckDigit();
}
public void paint (Graphics page){
// ---------- Start character
startStop(page);
// ---------- 1. MID digits
drawDigit(page, MID/100000);
drawDigit(page, MID/10000 -
MID/100000*10);
// or MID%100000 - MID%10000*10 >>
same idea can be applied below too
drawDigit(page, MID/1000 - MID/10000*10);
drawDigit(page, MID/100 - MID/1000*10);
drawDigit(page, MID/10 -
MID/100*10);
drawDigit(page, MID%10);
// ---------- Middle character
middle(page);
// ---------- 2. Item Number digits
drawDigit(page, itemNumber/10000);
drawDigit(page, itemNumber/1000 -
itemNumber/10000*10);
drawDigit(page, itemNumber/100 - itemNumber/1000*10);
drawDigit(page, itemNumber/10 - itemNumber/100*10);
drawDigit(page, itemNumber%10);
// ---------- 3. Check digit
drawDigit(page,
checkDigit);
// ---------- Stop character
startStop(page);
loc = 10;
black = true;
}
public void exitWindow (){
System.exit(0);
}
//
------------------------------------------------------
// Start and Stop Characters (
//
------------------------------------------------------
private void startStop (Graphics p){
bar(p); space(); bar(p);
black = !black;
}
//
------------------------------------------------------
// Middle Character (1-1-1-1-1)
[space-bar-space-bar-space]
//
------------------------------------------------------
private void middle (Graphics p){
space(); bar(p); space(); bar(p); space();
black = !black;
}
//
------------------------------------------------------
// Zero (0) = (3-2-1-1)
//
------------------------------------------------------
private void drawZero (Graphics p){
if(black){
bar(p); bar(p); bar(p);
space(); space();
bar(p);
space();
}
else{
space(); space(); space();
bar(p); bar(p);
space();
bar(p);
}
}
//
------------------------------------------------------
// One (1) = (2-2-2-1)
//
------------------------------------------------------
private void drawOne (Graphics p){
if(black){
bar(p); bar(p);
space(); space();
bar(p); bar(p);
space();
}
else{
space(); space();
bar(p); bar(p);
space(); space();
bar(p);
}
}
//
------------------------------------------------------
// Two (2) = (2-1-2-2)
//
------------------------------------------------------
private void drawTwo (Graphics p){
if(black){
bar(p); bar(p);
space();
bar(p); bar(p);
space(); space();
}
else{
space(); space();
bar(p);
space(); space();
bar(p); bar(p);
}
}
//
------------------------------------------------------
// Three (3) = (1-4-1-1)
//
------------------------------------------------------
private void drawThree (Graphics p){
if(black){
bar(p);
space(); space(); space(); space();
bar(p);
space();
}
else{
space();
bar(p); bar(p); bar(p); bar(p);
space();
bar(p);
}
}
// ------------------------------------------------------
// Four (4) = (1-1-3-2)
//
------------------------------------------------------
private void drawFour (Graphics p){
if(black){
bar(p);
space();
bar(p); bar(p); bar(p);
space(); space();
}
else{
space();
bar(p);
space(); space(); space();
bar(p); bar(p);
}
}
//
------------------------------------------------------
// Five (5) = (1-2-3-1)
//
------------------------------------------------------
private void drawFive (Graphics p){
if(black){
bar(p);
space(); space();
bar(p); bar(p); bar(p);
space();
}
else{
space();
bar(p); bar(p);
space(); space(); space();
bar(p);
}
}
//
------------------------------------------------------
// Six (6) = (1-1-1-4)
//
------------------------------------------------------
private void drawSix (Graphics p){
if(black){
bar(p);
space();
bar(p);
space(); space(); space(); space();
}
else{
space();
bar(p);
space();
bar(p); bar(p); bar(p); bar(p);
}
}
//
------------------------------------------------------
// Seven (7) = (1-3-1-2)
//
------------------------------------------------------
private void drawSeven (Graphics p){
if(black){
bar(p);
space(); space(); space();
bar(p);
space(); space();
}
else{
space();
bar(p); bar(p); bar(p);
space();
bar(p); bar(p);
}
}
//
------------------------------------------------------
// Eight (8) = (1-2-1-3)
//
------------------------------------------------------
private void drawEight (Graphics p){
if(black){
bar(p);
space(); space();
bar(p);
space(); space(); space();
}
else{
space();
bar(p); bar(p);
space();
bar(p); bar(p); bar(p);
}
}
//
------------------------------------------------------
// Nine (9) = (3-1-1-2)
//
------------------------------------------------------
private void drawNine (Graphics p){
if(black){
bar(p); bar(p); bar(p);
space();
bar(p);
space(); space();
}
else{
space(); space(); space();
bar(p);
space();
bar(p); bar(p);
}
}
//
------------------------------------------------------
// Draw Bar
//
------------------------------------------------------
private void bar (Graphics page){
page.drawLine(loc, startY, loc++, endY);
}
//
------------------------------------------------------
// Space
//
------------------------------------------------------
private void space (){
loc++;
}
//
------------------------------------------------------
// Draw Digit
// ------------------------------------------------------
private void
drawDigit(Graphics page, int digit){
switch(digit){
case 0: drawZero(page); break;
case 1: drawOne(page); break;
case 2: drawTwo(page); break;
case 3: drawThree(page); break;
case 4: drawFour(page); break;
case 5: drawFive(page); break;
case 6: drawSix(page); break;
case 7: drawSeven(page); break;
case 8: drawEight(page); break;
case 9: drawNine(page);
}
}
}
Question
2
//********************************************************************
// colors.java Author: R. Elias
//
// This applet draws a palette of colors
//********************************************************************
import java.applet.Applet;
import java.awt.*;
public class
Colors extends Applet
{
private final int APPLET_WIDTH = 768;
private final int APPLET_HEIGHT = 50;
public void init (){
setSize (APPLET_WIDTH, APPLET_HEIGHT);
}
public void paint
(Graphics page)
{
int loc = 0;
// From Red to Green through Yellow
for (int j=0; j<256; j+=2, loc++){
Color c = new Color(255, j, 0);
page.setColor (c);
page.drawLine (loc, 0, loc,
APPLET_HEIGHT);
}
for (int i=255; i>=0; i-=2, loc++){
Color c = new Color(i, 255, 0);
page.setColor (c);
page.drawLine (loc, 0, loc,
APPLET_HEIGHT);
}
// From Green to Blue through Cyan
for (int k=0; k<256; k+=2, loc++){
Color c = new Color(0, 255, k);
page.setColor
(c);
page.drawLine (loc, 0, loc,
APPLET_HEIGHT);
}
for (int j=255; j>=0; j-=2, loc++){
Color c = new Color (0, j, 255);
page.setColor (c);
page.drawLine (loc, 0, loc,
APPLET_HEIGHT);
}
// From Blue to Red through Magenta
for (int i=0; i<256; i+=2, loc++){
Color c = new Color(i, 0, 255);
page.setColor (c);
page.drawLine (loc, 0, loc,
APPLET_HEIGHT);
}
for (int k=255; k>=0; k-=2, loc++){
Color c = new Color (255, 0, k);
page.setColor (c);
page.drawLine
(loc, 0, loc, APPLET_HEIGHT);
}
}
}
//
=================================================================
<html>
<head>
<title>The
Colors Applet</title>
</head>
<body>
<applet
code="Colors.class" width=768 height=50>
</applet>
</body>
</html>