CS2102   Exam #3 Solutions

wpe478.jpg (20793 bytes)

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.

  1. An abstract class can have a constructor, but an interface cannot
  2. An abstract class can define instance variables, an interface cannot
  3. An abstract class can extend another class; an interface can (optionally) only extend another interface
  4. An abstract class can be defined with some methods with implementations; an interface is restricted to have no method implementations, just specification

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);
    }
}
  1. [8 points] In Pen class, the use of 'static' with width and height is inappropriate. This would enforce all Pen objects to have the same width and height, and the intent of the programmer is clearly to have this information associated with each Pen object.
  2. [4 points] If Pen extends Point, then it shouldn't have a loc field, since this information would be inherited as (x,y) from Point
  3. [4 points] If Pen extends Point, then the constructor taking (w,h) should not simply call super; rather, it should set width and height as appropriate.
  4. [4 points] In the Pen class, the toString() implementation has variable x referenced when it should be the string "x" or the character 'x'. The only reason this code works is because (miraculously) the Pen class inherits instance variable 'x' from Point.

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 can’t stop laughing

wpe493.jpg (12407 bytes)


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;
}