CSC 241- Exam 2        Name:


(35 Pts.) 1. Respond to the following short answer questions.

  1. What is an abstract class?

  2. When extending a class that is not abstract, such as Tank, do you have to redefine all of its methods?

  3. Assume our abstract class stringQueue. In an application that imports the stringQueue package, will it be legal to say stringQueue q = new stringQueue;? why?

  4. Why are you able to push Tank objects onto a genStack object or pop them of of it? Can a genStack object keep objects of other classes, such as, String or Button?

  5. Explain how one decides on using an array or a linked list for storing data. Give at least two reasons in support of each.

  6. What is the purpose of an Inductive Step in mathematical induction?

  7. Can one write a recursive method without a base case? why?


(15 Pts.) 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 as shown.

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

Now assume the following node class:

             
             class Node {
                int element;
                Node next;
                Node (Node n, int e) {
                  next=n;
                  element=e;
                }
             }
             
  1. What is the value of head.next.next.element?
  2. For the above list, write the Java instruction(s) that would create a node with 19 and place it between the nodes that hold 5 and 15. Keep in mind that head references the node with 5.


(15 Pts.) 3. Here is the class Tank:


public class Tank {
  public static int capacity=5000;
  protected int content_;
  public Tank()
  public Tank(int init_cont)  throws
    TankOverFlowException, IllegalArgumentException
  public int content () 
  public void add(int amount) throws
    TankOverFlowException, IllegalArgumentException 
  public void remove(int amount) throws
    TankUnderFlowException, IllegalArgumentException 

a) Write the following method for an application that must distribute the contents of two tanks evenly between them. For example, if tank1's content is 100 and tank2's is 50, when this method is done, they each will contain 75. If sum of the contents is an odd number, one tank will end up with 1 gallon more than the other when this method is done (for example, if tank1 contains 101 gallons and tank2 contains 50, tank1 should end up with 76 and tank2 with 75).

Keep in mind that content_ is protected and that add and remove throw exceptions.


     static void makeEqual (Tank tank1,Tank tank2)
     {






     }
b) State in English the postcondition for the makeEqual method. Be concise and complete.




(15 Pts.) 4. Assume that the following array variable L and the size variable exist and the method test2 has access to them. What answer will this method return if we call it with test2(0)?

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

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

(20 Points) 5. Assume the following Stack and Queue class definitions; note that they are NOT intStack and stringQueue, they are also not abstract:


public class Stack {
  public Stack ()
  public boolean empty()
  public boolean full() 
  public int pop()
  public void push(int val)
}
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 take elements of a queue (q) out one at a time and put them into a newly created stack (s) until there are no more elements in q. This method returns s when it is done. Don't worry about the stack becoming full.


     public Stack move_all(Queue q) {
         Stack s = new Stack();
         // design a loop to take each element from q and put in s









         return s;
     }