(20 Points) 1. Respond to the following true/false questions.
(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;
}
}
(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) {
…
}
}
public void makeEmpty () {
}
(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());}
}