4  Lab Tuesday pm:

Design methods, build a test suite, introduction to templates

Methods for simple classes and classes with containment

Remember the class Image from exercise 2 for creating Web pages. Develop the following methods for this class:
  1. isPortrait, which determines whether the image is taller than wider;

  2. size, which computes how many pixels the image contains;

  3. isLarger, which determines whether one image contains more picture than some other image.

Develop the following methods for the class House from exercise 2:
  1. isBigger, which determines whether one house has more rooms than some other house;

  2. thisCity, which checks whether the advertised house is in some given city (assume we give the method a city name);

  3. sameCity, which determines whether one house is in the same city as some other house.

Don't forget to test these methods.

Here is a revision of the problem of managing a runner's log (see figure 4):
Develop a program that manages a runner's training log. Every day the runner enters one entry concerning the day's run. ...For each entry, the program should compute how fast the runner ran. ...
Develop a method that computes the pace for a daily entry.


+-----------------+
| Entry           |
+-----------------+   +-----------+
| Date d          |-->| Date      |
| double distance |   +-----------+
| int duration    |   | int day   |
| String comment  |   | int month |
+-----------------+   | int year  |
                      +-----------+

Figure: An entry for a runner's log

Methods for composition

Note: The classes for the exercise  4 are in your handouts. Add the methods to these classes.

Recall the problem of writing a program that assists a book store manager (see exercise 2). Develop the following methods for this class:
  • currentBook that checks whether the book was published in 2003 or 2002;

  • currentAuthor that determines whether a book was written by a current author (born after 1940);

  • thisAuthor that determines whether a book was written by the specified author;

  • sameAuthor that determines whether one book was written by the same author as some other book;

  • sameGeneration that determines whether two books were written by two authors born less than 10 year apart.

Exercise 2 provides the data definition for a program that keep track of weather records. Develop the following methods:
  1. withinRange that determines whether today's high and low temperatures were within the normal range;

  2. rainyDay that determines whether the precipitation is higher than some given value;

  3. recordDay that determines whether the temperature broke either the high or the low record;

  4. warmerThan that determines whether one day was wormer than another day;

  5. lowerRecord that determines whether the record low for one day was lower than for some other day.