©2006 Felleisen, Proulx, et. al.
Work out the problem 6.1 in the textbook. Solution
Design the following methods for the shipment packages:
Determine whether this package weighs less than some given weight.
Determine the total volume of the package.
Is one package going to the same customer as another package?
Looking at this package and given another package, produce the one that has the smaller volume.
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.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.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.
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
We continue working with the classes that help us set up the recording of TV shows.
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.
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.
Is the time when one show ends before the time when another show
begins? (Method name: endsBefore
)
Does one show starts before another show starts? (Method name:
startsBefore
)
Is one show the same as another one, other than the time for
the recording? (Method name: sameShow
)
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
)