Lecture 15 Objectives [Nov 17]prev next

MUSICAL COMPOSITION: Goldberg Variations, var. 8-14 (from a total of 30), J. S. Bach, played by Glenn Gould.
 
9:07  var. 8-14


At the end of today's class you should

HAVE READ:

KNOW:

BE ABLE TO:

DAILY QUESTION:

package nov17;

import nov17.daily.Circle;
import nov17.daily.Figure;
import nov17.daily.Triangle;

public class FigureTest {

  /** Small program to show nuances for Figures. */
  public static void main (String[] args) {
    Figure circle = new Circle(2);
    Figure triangle = new Triangle (3, 4, 5);

    if (triangle.sameArea(circle)) {
       System.out.println ("Figures have same area");
    }
  }
}

And Figure

package nov17.daily;

/**
* Figure is a special class because its constructor has
* been marked as being protected.
*
* Thus you can't arbitrarily go and construct Figure instances.
*
* @author heineman
*
*/
public class Figure {

  /** Make available to subclasses ONLY this constructor. */
  protected Figure () {
  }

  protected double area;  /** Area of a figure. */

  /**
   * Return the area of the figure.
   *
   * @return area of the figure.
   */
  public double getArea () {
    return area;
  }

  /**
   * Set the area for the figure.
   *
   * Note that only Friends can access this method.
   *
   * @param newArea newly calculated area.
   */
   protected void setArea (double newArea) {
     area = newArea;
   }

  /**
   * Compare two figures by area
   *
   * @param f Figure to be compared against
   * @return true if the figures have the same area.
   */
  public boolean sameArea (Figure f) {
    return getArea() == f.getArea();
  }
}

and Circle

package nov17.daily;

/**
 * Represents a Circle which is a figure with a known area.
 *
 * @author heineman
 */
public class Circle extends Figure {

  /** Circle stores this information. */
  int radius;

  /**
   * Construct circle using radius
   *
   * @param r radius of circle
   */
  public Circle (int radius) {
    this.radius = radius;

    setArea (Math.PI*radius*radius);
  }

  /** Return String representation. */
  public String toString () {
    return "[Circle (" + radius + ") area=" + getArea() + "]";
  }
}

 

Sample Exam question(s)

1. There are three code samples. Which show evidence of overloading, overriding, polymorphism, static binding