COSC4301              Test-2           11/28/2011

Keys Used:

    1-5:     CDADA

    6-10:    BDEEA

 

Name:__________________________________________ 

 

PART-A:  Multiple Choice Questions. (30 points)

 

1.   Which is java.awt?

a. JFrame

b. JPanel

c. BorderLayout

d. Two above      e.  all above

2.   Which is in javax.swing?

a. JComponent

b. JMenu

c. JMenuItem

d. All above.     e.  two above

3.   Which is a subclass of the class JMenuItem?

a. JMenu

b. JMenuBar

c. JComponent

d. All above      e. two above.    f.  none above. 

4.    Which is a subclass of the class Container?

a. Component

b. JComponent

c. JFrame

d. Two above.     e.  none above.

5.   Which is an Interface?

a. ActionListener 

b. ActionEvent

c. GridLayout

d. Two above       e.  none above.

6.   Which is the postfix expression of “(A+B)+C*D”?

a.    AB+C+D*

b.    AB+CD*+

c.    ABC+D*+      d.  none above.

7.   final block in a method is executed when

a. An exception is thrown but not caught by the method.

b. An exception is thrown and caught by the method.

c. An exception is not thrown.

d. All above.    e. two above.      f. none above.

8.   Which is an unchecked exception?

a. RuntimeException

b. IOException

c. ArithmeticException

d. All above       e. two above.    f.  none above

9.   Which is a checked exception?

a. IOException

b. FileNotFoundException

c. InputMismatchException

d. All above.      e.  two above.   f.  none above

10.   Which is true of javascript?

a. It is case-sensitive.

b. Its functions can have any number of parameters all passed by address. 

c. It is to appear in the body part of HTML document.

d. All above.      e.  two above.   f.  none above.

 

Part-B: Other Problems.               (70 points)

Do three or four problems such that their total is 70 points.

 

For the first two problems, Consider the following declaration of Class BinarySearchTree and TreeNode (an inner class) for creating/manipulating binary search trees.

 

public class BinarySearchTree

{

  public class TreeNode      

      // an inner class of class BinarySearchTree, for binary search trees

      // 3 private instance variables

      {   private int item;

        private TreeNode leftLink;

        private TreeNode rightLink;

      // One constructor for TreeNode

      ///////////////////////////////////////////////////////

        public TreeNode(int newItem, TreeNode left, TreeNode right)

        {

             item      = newItem;

             leftLink  = left;

             rightLink = right;

        }

    }       //    End of TreeNode inner class

 

    // Declaration of class BinarySearchTree begins here.

    // Three instance variables.

    private TreeNode root;

    private TreeNode parent; // parent node of a target node being sought

    private int parentLink;         // pointer number of a parent node being sought

// 0: no parent, 1: left pointer pointing to the Left Child, 2: right

// pointer pointing to the Right child.

    // One no-argument constructor for creating an empty binary tree.

    //////////////////////////////////////////////////////////

    public BinarySearchTree( )

    {

        root = parent = null;

        parentLink = 0;             // no such pointer at the beginning

    }

 

Problem-A: (20 points)

 

Define another constructor whose heading is:

public BinarySearchTree (TreeNode rootNode)

This constructor will create a binary search tree which is a deep copy of an existing one whose root node is given by the lone parameter of this constructor.

Assume that this constructor will do the job simply by having the following statement:

      root =  copyBST (rootNode);

So, your job is really to define this method copyBST(root) instead which must be copying the given binary search tree and returning the resulting tree root node, preferably recursively.

 

Problem-B:  (20 points)

 

Define a method whose heading is:

          public TreeNode successor (TreeNode currentNode)

This method finds and returns the tree node whose item value immediately follows that of the given currentNode. Assume that the given currentNode always has its own right child, to make your job simple and easy.

 

 

The next two problems are about the Class Node and the Class NodeList that are used for linked list of Strings.

 

 

Problem-C:  (20 points)

 

Define a method whose heading is:

          public NodeList ReverseRec()

 

This method will simply reverse the calling linked list recursively and return the resulting reversed list. Note that the calling linked list may or may not sorted in any way.

 

 

Problem-D:        (20 points)

Assume that nodes of linked lists are sorted in an ascending order, that is, from small to large.

Define a method whose heading is:

          Public TreeNode searchLinkedList (String wanted)

This method will search the calling linked List for a node containing the given string (wanted) and return that node if found, or else it will return null.

Remember the nods of the current linked list are already sorted, which should make your job simpler.

 

Problem-E.  (30 points)

 

Define a GUI method whose heading will be:

          Public threePanels()

This method will construct one button and three panels such that (1) these four components will be added to the Jframe using GridLayout(4,1) and (2) each time the lone button is clicked a panel will be colored to one of three colors of your choice in a sequence while other panels will be light-gray. You can use a random number to pick the panel to be colored.

 

Problem-F.  (10 points)

 

Define a method whose heading is:

          public double diagonalMult (int size, double[][] square)

 

This method will calculate the product of all positive diagonal elements of the given square matrix of the given size and returns the final product.

 

Problem-G. (10 points)

 

Briefly describe some advantages of using stacks and common operations on them.

 

Problem-H:  (10 points)

 

Briefly compare three Layout managers of GUI Containers.