CSC 241- Exam 2        Name:

Fall 2002


(20 Points) 1. Respond to the following true/false questions.

  1. The fixed version of a Stack is used when you know the maximum number of objects that you may have to hold in it. True/False.
  2. In your assignment 2-part2, when we push a car onto a carRow, it gets placed in the carRow based on its carType automatically, yellow cars ahead of red ones and red ones ahead of black ones. True/False.
  3. Methods defined for an interface must have code in them in the interface itself. True/False.
  4. A class that implements an interface must have all method required by that interface. True/False.
  5. When you leave out the access qualifier for a component, such as, a field; it defaults to protected. True/False.
  6. private fields are accessible in the class, any class inheriting it, or any other class in the same package. True/False.
  7. moveableLight is an interface that is implemented by the car class. True/False.
  8. carRow is a class that extends the Stack interface. True/False.
  9. garage is a class that extends parkingLot. True/False.
  10. garageApplet  has a garage object. True/False.

(20 Points) 2. Consider the following conceptual representation for a linked list. head references the first node in the list.

                       -----      -----      -----         ----- 
                      | 434 |===>| 231 |===>| 111 |===>...| 234 |===||
                       -----      -----      -----         ----- 
                      head
 

Here is the definition of our node class:

 
             class Node {
                int element_;
                Node next_;
            Node (Node n, int e) {
                  next_=n;
              element_=e;
                }
             }
  1. What is head_.next_.element_ in the above list?

 

  1. Write the instruction to declare a temporary Node variable, named temp, and have it reference the 2nd node in the above list (the node with 231 in it). You should NOT construct a Node here.

 

 

  1. Assuming that step b) above has taken place and temp is referencing the node with 231. Write the instruction(s) that would create a node with the number 100 and place it between nodes holding 231 and 111. 




(20 Points) 3. Here is the interface for Stack:

 
public interface Stack {
  public boolean empty();
  public boolean full();
  public Stack push(Object x) throws stackException;
  public Stack pop() throws stackException;
  public Object peek();
}

Suppose we are writing an application that has a Stack. For this question, lets assume we don't know which implementation of Stack we have--fixed or dynamic.

Write the method removeTopTwo() that removes the top two elements of the stack.  This method should ensure that the stack does not change in any way, if it has only one element or is empty already.  Remember that the above methods are the only tools you have to work with.

 
static void removeTopTwo (Stack s) {
 
 
 
 
 
 
    
 
 
 
 
 
 
}
 
 
 
 
 
 
 
 
 
 
(20 Points) 4. The following is the skeleton for our carRow class used in assignment2-part2. carRow is a fixedStack. 
 

public class carRow extends fixedStack {

 

  public carRow (int size, int x, int y, Color rowColor){

  }

 

  public boolean hasCar(int ty) {

    for (int i=0;i<=top_;i++)

      if (((car)l_[i]).carType() == ty) return true;

    return false;

  }

 

  public car frontCar () {return (car)top();}

 

  public static void moveCars (carRow from, carRow to, int carType) {

  

  }

 

  public void draw(Graphics g) {

  

  }

}

 

 
  1. In the above skeleton I am sharing with you the code for hasCar and frontCar, so that you can see the carRow objects can be manipulated through their inherited Stack methods or directly through their l_ and or top_ fields that they also inherit from fixedStack.  Write a method to be added to carRow that would make the carRow object empty.

public void makeEmpty () {

 

}

 

  1. Suppose we have an application with a carRow variable (c_). Assuming that the makeEmpty method does not exist in carRow, write a code segment that would empty out c_.

 

 

(20 Points) 5. The following is the method enter() from the garage class used in assignment 2-2.  As you know the protocol that we employee for placing cars in a carRow forces us to try and put black cars in one of the first three rows first; if we don't succeed, we try to put a black car in row 5 which is designated for black cars. Change this code segment to first put a black car in row 5 if it has room, and then, if there is no room there to attempt to place it in one of the first three rows.

  public void enter (car c) {

    try{

      for (int i=row1;i<=row3;i++)

        if (c_[i].empty() ||

            (!c_[i].full() && c.carType()<=c_[i].frontCar().carType()))

        {

          c_[i].push(c);

          repaint();

          return;

        }

      if(c.carType()==3 && !c_[blackRow].full()) {

        c_[blackRow].push(c);

        repaint();

        return;

      }

      if(c.carType()==2 && !c_[redRow].full()) {

        c_[redRow].push(c);

        repaint();

        return;

      }

    }

    catch(stackException e) {System.err.println(e.getMessage());}

  }