©2005 Felleisen, Proulx, et. al.
Methods for Classes
Remember the class Image
from exercise 1.6
for
creating Web pages. Design the following methods for this class:
isPortrait
, which determines whether the image is taller
than wider;
size
, which computes how many pixels the image contains;
isLarger
, which determines whether one image contains more
pixels than some other image.
Also draw a complete class diagram.
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:
"small"
for images with 10,000 pixels or fewer;
"medium"
for images with between 10,001 and 1,000,000 pixels;
"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.
Design a method that computes how long it will take to download an image at our internet access speed (given in bytes per second).
Design a method that determines whether you can download an image within the limited time, again, knowing the download speed.
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.