How to Design Class Hierarchies: Lecture Notes

Viera K. Proulx and Matthias Felleisen

January 12, 2003

0.1  Week 1 - Lecture 3: As You Like It

In some programs the computational formula is different for each of several sets of input values. For example, the price of admission to a museum is different for children, youth, adults, and seniors. Children get in for free, youth and seniors pay half price, adults pay $10.00. The design recipe for this type of functions introduces a new step. Obviously, the data the function ticketPrice consumes is the age of the patron (an int). The result is the price in dollars (a double). Before developing the body of the function, we create a template for the function body. For each condition, there is one entry:

    /* Template */

        if (age < 6)
            return ...
        if (age < 16)
            return ...
        if (age < 65)
            return...
        else
            return...

It is important to return to examples and tests to make sure there is at least one example/test for each condition, and preferably one test for each boundary between conditions as well. For this function at least four examples are needed (resulting in child, youth, adult, and senior prices). Preferably three more tests for ages 6, 16, and 65 are also included. The development of the body easily follows. The completed function is:

    /* compute the price of museum admission, given the age of patron
       ------------------------------------------------------------*/
    double ticketPrice(int age){
    /* Template
        if (age < 6)
            return ...
        if (age < 16)
            return ...
        if (age < 65)
            return ...
        else
            return...
     */

        if (age < 6)
            return 0.0;
        if (age < 16)
            return 5.00;
        if (age < 65)
            return 10.00;
        else
            return 5.00;
     }

    /* Examples/Tests:
     --------------------------------------------------------------*/
    void ticketPriceTests(){
        testHeader("ticketPrice(int age)");

        expected( 0.0);
        actual  ( ticketPrice(3) );

        expected( 5.0);
        actual  ( ticketPrice(12) );

        expected( 10.0);
        actual  ( ticketPrice(34) );

        expected( 5.0);
        actual  ( ticketPrice(80) );

        expected( 5.0);
        actual  ( ticketPrice(6) );

        expected( 10.0);
        actual  ( ticketPrice(16) );

        expected( 5.0);
        actual  ( ticketPrice(65) );
    }

The price of admission may be reduced for museum members:



age member non-member
under 6 free free
under 16 $3.00 $5.00
under 65 $5.00 $10.00
65 and over $3.00 $5.00



The template needs to reflect that:

    /* Template
        if (member){
            if (age < 6)
                return ...
            if (age < 16)
                return ...
            if (age < 65)
                return ...
            else
                return...
        }
        else{
            if (age < 6)
                return ...
            if (age < 16)
                return ...
            if (age < 65)
                return ...
            else
                return...
        }
     */

The { and } brackets enclose a body of code that is to be performed for the outer if statement and the outer else statement.

Last modified: Sun, Jan 12, 2003, 5:28 pm
HTML conversion by TeX2page 4q4