CSC 241- Exam 2        Name:


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

  1. We can store Objects of any class in intList objects. True/False.
  2. The fixed version of a Stack is needed when you don't know how many objects you may have to hold in it. True/False.
  3. In your assignment 2-part2, when adding a tank to the queue, it gets placed in the queue in the order of its content, after tanks with less content and before tanks with more. True/False.
  4. You may write a constructor for an interface in java. True/False.
  5. You may write a constructor for a class in java. True/False.
  6. orderedIntList only inherits the insert and insertAtBeginning methods in intList. True/False.
  7. When you leave out the access qualifier for a component, such as, a field, it defaults to public. True/False.
  8. protected fields are accessible in the class, any class inheriting it, or any other class in the same package. True/False.
  9. displayTank class is a subclass of Tank and the super class for changeableTank. True/False.
  10. changeableTank can not override any methods from displayTank. 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, 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;
                }
             }
  1. What is head_.next_.previous_.element_ in the above list?

 

  1. Write the instruction to declare a temporary Node variable and have it reference the 1st node in the above list. You should NOT construct a Node here.

 

 

  1. For the above list, write the Java instruction(s) that would create a node with the number 222 and place it between nodes holding 434 and 231. Make sure the existing links are updated.

 

 

  1. Write the instructions that would display the numbers in the linked list starting with the node referenced by head_. Start a temporary variable at the node referenced by head_ and iterate through the nodes, displaying each number stored.



    Here are the definitions of node and intList classes from lab4:
 
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)
}
 
  1. Write a method to be added to changeableTank that would return facing_; name this method facing.





  2. Suppose we have an application with a changeableTank (t_). Write a loop that keeps turning the tank, until it is facing UP.