Q1. [20 points] This question tests your knowledge of Java. Circle your answers True or False below.
Q2. [15 points] Describe at least three differences between an abstract class and a Java interface.
Q3. [20 points] There are at least five logical errors in the following code. The intent of the programmer is to design a concept called Pen that can paint into a Graphics device using arbitrary pixel width & height. Each Pen instance uses a point to know its location within the Graphics device. The PenDriver class is intended to test the class, but when run it only prints out pen[030] @ (10,30). Circle the errors and suggest fixes.
/** Represents a point in the graphical plane. */ public class Point { int x; int y; /** Default constructor. */ Point (int x, int y) { this.x = x; this.y = y; } public String toString () { return "(" + x + "," + y + ")"; } } |
/** Represents a pen that has width and height in pixels. */ public class Pen extends Point { static int width; /** Each pen knows its width. */ static int height; /** Each pen knows its height. */ Point loc; /** Each pen knows its location. */ /** Constructor is given initial (w,h) values. */ public Pen (int w, int h) { super (w,h); } /* Return string for object, like "pen[3x7] @ point (10,30)" */ public String toString () { return "pen[" + width + x + height + "] @ " + loc; } } |
public class PenDriver { public static void main(String[] args) { /** Construct Pen to be 3x7 @ point (10,30) */ Pen p = new Pen (3,7); p.loc = new Point (10,30); System.out.println (p); } } |
Q4. [15 points] This question tests more advanced Java concepts
Q5. [10 points] We recommended that GUI applications be divided into three sets of classes. Describe their responsibilities.
The responsibility of knowing when to refresh the view could be done in one of two ways (1) the Controller is responsible for updating the model and then alerting the view that it must refresh itself (at which point the View pulls the latest information from the Model); or (2) the Controller updates the model, who is then responsible for alerting potential views that it has changed. The view could be pushed the new changes or could, once again, pull the appropriate data from the Model.
Q6. Extra points if milk comes out of my nose because I cant stop laughing
Q7 (10AM). [20 points] You have been asked to write a method for the BinaryTree class that
returns the height of a binary tree. The height is
determined by the longest path from the root node to any node in the tree. The following
example trees are provided to help explain the concept.
![]() |
![]() |
public class BinaryTree { /** Value as Integer. */ Integer value; /** Left & Right Children */ BinaryTree left; BinaryTree right; } |
#leaves = 1 height = 1 |
#leaves = 2 height = 3 |
/** * Returns the height of a BinaryTree. * REQUIRES: none * MODIFIES: none * EFFECTS: returns int height */ public int getHeight() { // base case if ((left == null) && (right == null)) { return 1; } // recursive cases int ht = 1; if (left != null) { ht = 1 + left.getHeight(); } if (right != null) { int htRight = 1 + right.getHeight(); if (htRight > ht) { ht = htRight; } } return ht; } |
Q7 (1 PM). [20 points] You have been asked to write a method for the BinaryTree class that returns the number of leaves in the tree. A leaf node is one that has no left or right children are null. The following example trees are provided to help explain the concept.
/** * Returns the number of leaves in a BinaryTree * REQUIRES: none * MODIFIES: none * EFFECTS: returns int number of leaves */ public int numLeaves() { // base case if ((left == null) && (right == null)) { return 1; } // recursive cases int ct = 0; if (left != null) { ct += left.numLeaves(); } if (right != null) { ct += right.numLeaves(); } return ct; } |