CSC 241- Exam 2        Name:


(20 Points) 1. Respond to the following short answer questions.

  1. Can you construct an object of an abstract class?



  2. Can genStack (used in your assignment #2 for holding Tanks) be used to hold other object, like Buttons? Explain.



  3. When would you use an Array implementation of intStack instead of a dynamic implementation?



  4. What is the purpose of a base case in a recursive method?




(20 Points) 2. Consider the following conceptual representation for a linked list. head references the first node in the list with each node pointing to the next and previous as shown.

                       --       --       --       --       --
                 ||===|5 |<===>|15|<===>|-8|<===>|3 |<===>|6 |===||
                       --       --       --       --       --
                      head

Now assume the following node class:

             
             class Node {
                int element;
                Node next;
                Node prev;
                Node (Node n, Node p, int e) {
                  next=n;
                  prev=p;
                  element=e;
                }
             }
             
  1. What is the value of head.next.element?

  2. What is the value of head.next.prev.element?

  3. For the above list, write the Java instruction(s) that would create a node with 19 and place it before 5. As shown above, assume that head references the node with 5.



(20 Points) 3. Assume that the following array variable L and the size variable exist and the recursive method test2 has access to them. What answer will this method return if we call it with test2(0,-1)?

        --------------------------------------------
     L | 134    | 322    |   88   |  343   |  11    |  size=5
        --------------------------------------------
         0        1        2        3        4

     int test2 (int i, int max) {
       if (i==size) return max;
       if (L[i]> max)
          return test2(i+1,L[i]);
       else
          return test2(i+1,max);
     }

(20 Points) 4. Assume the following Queue class definitions:


public class Queue {
  public Queue ()
  public boolean empty()
  public boolean full() 
  public void enqueue(int val)
  public int dequeue()
}

Complete the following method which is suppose to return a duplicate of its queue parameter without destroying its content. Take elements of q out one at a time and put them into two different newly created queues (r, and t). This method must then move all elements of t back into q before returning r.


     public Stack move_all(Queue q) {
         Queue r = new Queue();
         Queue t = new Queue();










         return r;
     }


(20 Points) 5. Assume the following intList class definitions:


public class intList {
  public intList()
  public boolean empty()
  public int find(int val) 
  public void append(int val) 
  public int addBefore(int pos, int val)
  public int addAfter(int pos, int val) 
  public int remove(int pos) 
  public int valAt (int pos) 
  public int last()
}

Complete the following method which must return a portion of list L as identified by the from and to positions. Nothing should be removed from L, just the correct portion added to temp. Assume that from and to are valid positions in L.

     public intList subList(intList L, int from, int to) {
         intList temp = new intList();











         return temp;
     }