©2006 Felleisen, Proulx, et. al.

2  Self-Referential Data; Methods for Simple Classes.

Etudes

2.1  Etude

Work out the problem 6.1 in the textbook.  Solution

2.2  Etude

Design the following methods for the shipment packages:

 Solution

Main Assignment -- Part 1

2.1  Problem 6.7 in the text HtDC.

2.2  Problem (6.9)

Consider the following puzzle:

A number of people want to cross a dilapidated bridge at night. Each person requires a different amount of time to cross the bridge. Together they have one battery-powered flashlight. Only two people can be on the bridge at any given time, due to its bad state of repair.

Given the composition of the group and the life-time of the battery, the problem is to determine whether and how the entire group can cross the bridge.

Solving the puzzle (manually or with a program) requires a recording of what happens as members of the group moves back and forth across the river. Let's call the status of the ``puzzle world'' after each individual crossing a state.

Design a data representation for the states of the puzzle. Which elements does a state have to record? What is the initial state for this problem? Express it in your representation. What is the final state for this problem? Solution

Main Assignment -- Part 2

We continue working with the classes that help us set up the recording of TV shows.

2.3  Problem

The program that keeps track of the programs you wish to record has to make sure your choices are valid. To do so, it needs to be able to answer the questions given below:

To make sure your data definitions are flexible enough, make sure that the date of the show and the time of the show are represented as two different fields. The class ShowTime then combines the day and the time of the show. Finally, the class TVshow also includes the title of the show and the channel on which it is being shown.

As you work through the problems, pay attention to the one task one method rule and delegate the work to the class that should be responsible for providing the answer.

  1. What is the total recording time for a show?

    Decompose this problem into the following parts:

    • Design the method minutes in the class ClockTime that computes the minutes since midnight that this time instance represents.

    • Design the method duration in the class TimeSlice that computes the duration of this time slice in minutes.

    • Design the method duration in the class ShowTime that computes the duration of this show in minutes.

    • Design the method recordingTime in the class TVshow that determines the recording time for this show.

    Once you are comfortable with designing methods, you may combine some of these steps. The purpose of this exercise is to show how to properly delegate the responsibility for a task to the class that can correctly handle the request. This design allows us to change the way the time is represented, or the way that the time slice is represented, without affecting the rest of the program. For example, the time slice may be represented by its starting time and its duration in minutes -- yet the TVshow class will not be affected by this change.

    Note: In the remaining exercises in this part make sure you follow a similar design strategy as above: each task should be delegated to the class that can correctly handle the srequest. At this point, no shortcuts are allowed.

  2. Is the time when one show ends before the time when another show begins? (Method name: endsBefore)

  3. Does one show starts before another show starts? (Method name: startsBefore)

  4. Is one show the same as another one, other than the time for the recording? (Method name: sameShow)

  5. You find out that your favorite show has been rescheduled and you know the new starting and ending time, but still on the same day. Design the method that produces the new show recording request from the original one. (Method name: newTime)

 Solution

Last modified: Tuesday, January 23rd, 2007 5:27:39pm