CSC 241- Assignment 2


Part 1 -- parkingLotObserver (Due 10/8/ 2003)

You will develop three components in this part. squareLight class extends the Light class from the previous assignment. squareLights represent car slots in a graphical observer for a parking lot. parkingLotObserver class depicts a parking lot; the activities (Enter/Exit) in the parking lot being observed cause modifications in the image representing it. Each time an enter or an exit occurs in the parking lot being observed, a car slot is turned on or turned off, respectively. Since we are using the parkingLot class that you developed in lab#2, you really don't know which car slot had been filled or emptied. You will be given a protocol to follow to depict enter or exit actions of a parkingLot. parkingLotApplet is an applet that allows a user to request entering cars into a parking lot or have them exit. parkingLotApplet uses a text field and a parkingLotObserver to show the status of the parking lot.

1.1. squareLight Class

Needs to extend Light and override the draw() method. This new class inherits all fields and the other methods of Light. The constructor simply invokes Light's constructor. The draw() method must draw a square, instead of a circle; it also needs to frame this filled-in square by drawing a black square around it.

1.2. parkignLotObserver Class

We like to keep track of a parkingLot object graphically. The parkignLotObserver will be designed to depicts activities of the parking lot. Initially, parkingLotObserver can only observe and graphically show the Enter action on a parkingLot; you will then test this version with the applet provided. Here is the skeleton for this class; each time you see ..., expect that there is something for you to do:


public class parkingLotObserver extends Canvas {
  protected static final Color row1 = Color.blue;
  protected static final Color row2 = Color.cyan;
  protected static final Color row3 = Color.green;
  protected static final Color row4 = Color.magenta;
  protected static final Color row5 = Color.orange;
  protected static final Color backGroundColor = Color.white;
  protected static final int slotSize = 30;
  protected static final int rows = 5;
  protected static final int carsPerRow = 9;
  protected static final int distanceBetweenRows = 30;
  protected static final int X_forTheFirstCar = 30;
  protected static final int Y_forTheFirstCar = 3;
  protected static final int maxCap = 45;

  /** parkingLot to observe **/
  parkingLot p_;

  /**Array of carslots; each car slot is represented as a squareLight**/
  squareLight [] carSlots_;
  
  /**Which slot to Enter into**/
  int back_=-1;

  /** The previous parkingLot count before a change. **/
  int lastCount_=0;

  /** Get reference to the parkingLot to observe, and initialize
      all car slots. 
  **/
  public parkingLotObserver (parkingLot p) {
    setBackground(backGroundColor);

    if (p.capacity() != maxCap) {
      System.err.println(p.capacity() + " not equal to max capacity " +
                          maxCap);
      System.exit(-1);
    }

    p_=p;

    //construct the carSlots_ array and initialize all cells
    //with the correct coordinates, size, and onColors. 
    carSlots_ = new squareLight [p_.capacity()];
    for (int i=0;i < carsPerRow;i++) {
      carSlots_[i] = new squareLight (slotSize,row1,
                                      X_forTheFirstCar+i*slotSize,
                                      Y_forTheFirstCar);
    }
    for (int i=0;i < carsPerRow;i++) {
      carSlots_[carsPerRow+i] = new squareLight (slotSize,row2,
                                      X_forTheFirstCar+i*slotSize,
                                      Y_forTheFirstCar+2*slotSize);
    }
    for (int i=0;i < carsPerRow;i++) {
      carSlots_[carsPerRow*2+i] = new squareLight (slotSize,row3,
                                      X_forTheFirstCar+i*slotSize,
                                      Y_forTheFirstCar+3*slotSize);
    }

    //Set up rows 4 and 5.
    ...

    setSize(X_forTheFirstCar+carsPerRow+slotSize+5, 
            Y_forTheFirstCar+8*slotSize);
  }

  /**Return the parkingLot being observed. **/
  public parkingLot p () {return p_;}

  /**Return lastCount remembered by the observer. **/
  public int lastCount () {return lastCount_;}

  /**Draw the image of the parking lot. **/
  public void paint (Graphics g) {
    //exit abnormally if carCount has changed by more than 1.
    ...

    //draw a border, set color to black, use getWidth() and getHeight() for 
    //width and height.
    ...
 
    //Increment back_; turn on the carSlot at cell back_;  
    //set lastCount_ to p_'s carCount.
    ...

    //draw all slots
    ...
  }
}

Use the applet parkingLotApplet.html to test your class. Download parkingLotApplet.java into your parkingLot directory along with its .html file.

1.3. Complete parkingLotObserver and the Applet

parkingLotObserver in its initial development observed Enter operations only. Obviously, our parkingLot class is designed to have both Enter and Exit functions. parkingLotObserver needs to be modified to handle Exits as well.

parkingLotApplet needs to be modified by adding an exit button to the top panel. When this button is clicked, invoke the exit() method on the parkingLot and then repaint() the parkingLotObserver; it should follow a similar pattern as Enter. Here is how mine works: parkingLotApplet.

EXTRA CREDIT

Provide the functionality through two buttons to Empty or Fill the parkingLot in the parkingLotApplet. This Extra credit will require additional buttons handling of those buttons in the Applet. Also, the paint() method in parkingLotObserver should no longer need a "changed too much" concept handled in it; in other words, regardless of how many cars have entered or exited the parkingLot since the last repaint() call, the observer will do the right thing and NOT exit abnormally.

What/How to turn in for Part#1

Email your .java files (squareLight.java, parkingLotObserver.java, and parkingLotApplet.java) to me as attachments. There should be links to your applets for testing squareLight, as well as, your updated parkingLotApplet in your 241 page.

be sure to do a chmod 755 *.class in your parkingLot, directory; also, perform a chmod 755 *.html in that directory. your parkignLot directory has to be made public as well.


Part 2 -- parkingGarage (Due 10/31/03)

We are simulating a New York city parking garage. To do this we need a few components built.

Initial setup

2.1. class moveableLight--Recommended to be completed by (Due 10/24/03)

moveableLight extends squareLight and should be in the parkingGarage directory; package it like its super class. The constructor for moveableLight should look like squareLight's constructor; this class provides the following two new methods that now let us change the coordinates of a light object, don't declare any fields for moveableLight as it already has inherited all the fields that it needs:

/**Change the light's X-coordinate**/
public void changeX (int x){...} 

/**Change the light's Y-coordinate**/
public void changeY (int y){...} 

To test moveableLight, do the following:

2.2. class car--Recommended to be completed by 10/27/03

A car in this context doesn't have to look like one, we really don't care about the model or make of the car either. We simply care about how long a car is expected to be parked at our garage. We categorize cars into three types:

  1. Cars that stay under 3 hours.
  2. Cars that stay between 3 to 6 Hours.
  3. Cars that stay longer than 6 Hours.

car also has a squareLight field (light_). All cars have a size of 30X30 in our program. At construction, a car, gets its x and y coordinates along with an int parameter (flag) that has 3 possible values (1,2, or 3) corresponding to the possible car types that we can have.

light_ gets constructed in the car's constructor:

Here is skeleton of a car class, the .java file needs to be in your parkingGarage directory:


package csc241.parkingGarage;

import java.awt.*;

public class car  {
  public static final Color shortStay = Color.yellow;
  public static final Color mediumStay = Color.red;
  public static final Color longStay = Color.black;
  protected static final int carSize = 30;

  protected int carType_;
  protected moveableLight light_;

  /* construct light_ based on carType and set cartype_*/ 
  public car (int carType, int x, int y){...}

  public moveableLight light() {return light_;}

  public int carType () {return carType_;}
}

To test car, do the following:

2.3 carRow

A carRow is a holder of cars where we wants each car to be put in front or removed from the front. carRow extends fixedStack so it acts as a Stack providing you with push(), pop(), top(), empty(), and full(). carRow adds a couple of features beyond what we get from fixedStack that are useful to us in our project:

Download carRow and do the following to fixedStack and carRow:

2.4 garage

garage conceptually is a combination of a parking garage and its observer in just one class. Even though, separating a garage and its observe is the more acceptable design pattern; it will be easier to implement them as one entity. A garage maintains five rows of cars, with up to 10 cars in each row. These rows are depicted vertically and are adjacent to one another; unlike the parkingLotObserver where there was space provided for cars to drive out.

We have one simple objective in mind, we don't want our attendants spend all their time moving cars around. So, we never put a black car (type 3--long stay) in front of a red car (type 2--medium stay) or yellow car (type 1--short stay), and a red car will never get put in front of a yellow car.

There are obviously many ways one could accomplish this; in real simulation projects we compare and contrast many strategies. But, in this case, the protocol that our simulation follows for entering and exiting cars is as follows:

This is what we do when a wants to enter the garage:

The above protocol is already been implemented for you, just download garage into your parkingGarage directory and make the appropriate package name change and the import change. Your task is to implement the following protocol for exists. Keep in mind that you have some very useful tools in your carRow class:

2.5 garageApplet

download garageApplet.java and its .html file: garageApplet. This version only allows cars to enter the garage, your task is to implement the exit. Keep in mind that both the packaging and the import in the .java file has to updated, as well as, the codebase and the code fields of the applet tag in the .html file. Be sure to make your .class file and the .html file public. Your applet after adding the concept of exit to both garage and garageApplet should work like: garageAppletAnswer.

What/How to turn in for Part#2

Email your .java files (moveableLight.java, car.java, garage.java, and garageApplet.java) to me as attachments. There should be links to your test applets for moveableLight and car, as well as, your updated garageApplet in your 241 page.

be sure to do a chmod 755 *.class in your Applets, parkingGarage, and Stack directories. Also, perform a chmod 755 *.html in your Applets directory. These directories themselves have to be made public as well.