(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, note that each node will have a reference to both the next and the previous nodes:
class Node {
int element_;
Node next_;
Node previous_;
Node (Node n, Node p, int e) {
next_=n;
previous_=p;
element_=e;
}
}
public class node {
int element_;
node next_;
node (int e, node n)
}
public class intList
{
node header_;
node current_;
public intList( )
public boolean isEmpty( )
public void makeEmpty( )
public void insert( int x ) throws Exception
public void insertAtBeginning( int x )
public boolean find( int x )
public void remove( int x ) throws Exception
public boolean validCurrent( )
public int retrieve( ) throws Exception
public void first( )
public void advance( )
public String dump ()
}
(10 Points) 3.Write an int method retrieveNext
to be added to the list of methods above in the intList class. This
method throws an exception if current_ or current_.next_ are
null; otherwise, it returns the element in the node after the one referenced by
current_.
(10 Points) 4. Suppose we have an application
that maintains a list of salaries in an intList variable (list)
with values like 10000, 40000, etc. Understand that you are in an application
and have no access to current_ or header_ of list. Write a
code segment that loops through salaries using the iterator methods (first,
advance, retrieve, and validCurrent) to locate the largest salary in our list
and displays it.
(20 Points) 5. Here is the interface for Queue and the Stack:
public interface Queue {
public boolean empty();
public boolean full();
public void enqueue(Object x) throws Exception;
public void dequeue() throws Exception;
public Object peek() throws Exception;
}
public interface Stack {
public boolean empty();
public boolean full();
public void push(Object x) throws Exception;
public void pop() throws Exception;
public Object peek() throws Exception;
}
Suppose we are writing an application that has a queue object. For this
question, lets assume we don't know which implementation of Queue we have,
fixedQueue or dynamicQueue.
Write the method copyQueueToStack() for our application that would
move all elements of the queue into a stack and return the stack.
static Stack copyQueueToStack (Queue q) {
/* which kind of stack makes more sense to construct here, fixed or dynamic? Construct one here:
*/
Stack s=
/* Write the instructions that remove all elements of the queue,
one at a time, and push them on to the stack. Keep in mind that you
need to handle the exceptions.
*/
return s;
}
(20 Points) 5. The following is the skeleton for our changeableTank class used in assignment2-part1. changeableTank inherits displayTank, which in turn, inherits Tank.
import java.awt.*;
public class changeableTank extends displayTank {
public static final int UP=1;
public static final int RIGHT=2;
public static final int DOWN=3;
public static final int LEFT=4;
protected boolean big_=true;
protected int facing_=UP;
protected boolean right_= true;
public changeableTank(int x, int y) {
super(x,y); //Invoke the class displayTank's constructor
}
public boolean right()
public boolean big()
public void switchSize()
public void changePosition(int x, int y)
public void setDirection(boolean right)
public void turn()
public void display (Graphics g)
}