Rice University - Comp 212 - Intermediate Programming

Fall 2001

Lecture #2 - The Best Little Pizza House in Texas (part 2)

Today's menu:

  1. Discussion of the syllabus.
  2. Back to the Pizza deal!

An Object-Oriented Programming (OOP) solution

A pizza has price and a shape.  We want to compute the price/area ratio.  It is the shape that intrinsically "knows" how to compute its area.  A shape is an abstract concept.  The area of a shape is an abstract concept.  A rectangle is a concrete and specific shape.  A rectangle knows its own width and height and therefore can compute its area.  A circle is a concrete and specific shape.  A circle knows its own diameter and therefore can compute its area.  The following table shows how this model of pizza is represented in Java and compares it with an equivalent Scheme representation.

OO Design in Java Data-directed Design in Scheme
/**
*
A pizza has a price and a shape.
*/

public class Pizza {
  private double _price;
  private AShape _shape;
  public Pizza(double p, AShape s) {
    _price = p;
    _shape = s;
  }
  public double getPrice() {
    return _price;
  }
  public AShape getShape() {
    return _shape;
  }
}
;; A Pizza is 
;;   (make-Pizza p s) where
;; p is a number representing the price,
;; and s is a Shape
(define-struct Pizza(price shape))
;; make-Pizza, Pizza-price, Pizza-shape
;; are automatically generated.
/**
*
A shape is an abstract entity that intrinsically knows how to compute its area.
*/
public abstract class AShape {
  public abstract double getArea();
}
;; A Shape is either
;;   (make-Rectangle w h) where
;; w and h are numbers representing
;; the width and the height
;; or
;;   (make-Circle r) where
;; r is a number representing the radius
;; contract: getArea: Shape -> number
;; purpose: 
;; (getArea s) returns the area of s.
/**
*
A rectangle is a shape.
* It has a width and a height and
* a way of computing its area using its width and height.

*/
public class Rectangle extends AShape {
  private double _width;
  private double _height;
  public Rectangle(double w, double h) {
    _width = w;
    _height - h;
  }
  public double getArea() {
    ... _width ...
    ... _height ...
  }
}
(define-struct Rectangle(width height))
;; make-Rectangle, Rectangle-width,
;; Rectangle-height
;; are automatically generated
/**
*
A circle is a shape.
* It has a radius and a way of computing its area using its radius.

*/
public class Circle extends AShape {
  private double _radius;
  public Circle(double r) {
    _radius = r;
  }
  public double getArea() {
    ... _radius ...
  }
}
(define-struct Circle( radius))
;; make-Circle, Circle-radius
;; are automatically generated
  ;; template
;; (define getArea (s)
;;    (cond [(Rectangle? s)
;;            ... (Rectangle-width s) ...
;;            ... (Rectangle-height s) ...]
;;          [(Circle? p)
;;            ... (Circle-radius s) ...]))
public class Rectangle extends AShape {
  // ...
  public double getArea() {
    return _width * _height;
  }
}
;; actual code
(define getArea (s)
   (cond [(Rectangle? s)
             (* (Rectangle-width s)
                (Rectangle-height s))]
         [(Circle? s)
             (* PI (Circle-radius s)
                   (Circle-radius s))]))
public class Circle extends AShape {
  // ...
  public double getArea() {
    return Math.PI *
_radius * _radius;
  }
}

 

Unified Modeling Language (UML) Diagram

UML is a de-facto standard for diagramming OO designs.  The above Pizza design is represented in UML as follows.