©2005 Felleisen, Proulx, et. al.

Methods for Classes

2.7  Problem (10.2.1)

Remember the class Image from exercise 1.6 for creating Web pages. Design 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 pixels than some other image.

Also draw a complete class diagram.

2.8  Problem (10.4.2)

Take a look at this following class:

// represent information about an image
class Image {
  int width;
  int height;
  String source;

  Image(int width, int height, String source) {
    this.    width = width;
    this.    height = height;
    this.    source = source;
  }
}

Design the method sizeString for this class. It produces one of three strings, depending on the number of pixels in the image:

  1. "small" for images with 10,000 pixels or fewer;

  2. "medium" for images with between 10,001 and 1,000,000 pixels;

  3. "large" for images that are even larger than that.

Remember that the number of pixels in an image is determined by the area of the image.

2.9  Problem

Design a method that computes how long it will take to download an image at our internet access speed (given in bytes per second).

2.10  Problem

Design a method that determines whether you can download an image within the limited time, again, knowing the download speed.

2.11  Challenge Problem (10.4.3)

Your physics professor would like to simulate an experiment involving bouncing balls. Design a class that represents a ball that is falling on a 10 x 100 canvas at a rate of DELTA. That is, each time the clock ticks, the ball drops by DELTA pixels.

When the ball reaches the bottom of the canvas, it bounces, i.e., it reverses course and travels upwards again. The bounce is perfect. This means that when the ball always travels the full distance. As long as it is far enough away from the ground, it drops the full distance. If it is too close, it drops by whatever is left and then travels upwards by the remaining number of pixels. Also, when it travels upwards it travels at the same rate as when it is falling.

Design the method move, which simulates one step in the movement of the ball.