CS
1401 Introduction to Computer Science
Fall 2013, Lab 7
Objective. The purpose of this lab is to practice defining and testing methods.
Assignment: Part 1. Write a method drawHline which
draws a horizontal line of a given color. This method should have
the following 5 input parameters:
- an image (of type
JRaster);
- starting x-coordinate;
- ending x-coordinate;
- y-coordinate;
- color of the line.
Assume that the
ending x-coordinate is larger than or equal than the starting
x-coordinate. Return an error message if the line is outside the
standard JRaster 200 x 200 box.
Assignment: Part 2. Write a method drawVline which draws a vertical line of a given color.
This method should have the following 5 input parameters:
- an image (of type JRaster);
- x-coordinate;
- starting y-coordinate;
- ending y-coorinate;
- color of the line.
Return an error message if the line is outside the standard box 200 x 200.
Assignment: Part 3. Write a method drawLine which
draws a generic line of a given color. This method should have the
following 6 input parameters:
- an image (of type JRaster);
- starting x-coordinate;
- starting
y-coordinate;
- ending x-coordinate;
- ending y-coordinate;
- color of the line.
Return an error message if the line
is outside the standard box 200 x 200.
Hint: A line which connects points (x1,
y1) and (x2, y2), with
x1 < x2, can be described as follows:
- When the line is more horizontal than vertical, i.e., when
|y2 − y1| <=
|x2 − x1|, then the line can be described by the formula
y = y1 + s * (x − x1),
where the slope s is equal to the ratio (y2 −
y1)/(x2 − x1). To draw the
line, for each x from the starting x-coordinate to the ending
x-coordinate, compute the corresponding value y and draw the point
with coordinates (x, y).
- When the line is more vertical than
horizontal, i.e., when |y2 − y1| >
|x2 − x1|, then the line can be described by the formula
x = x1 + S * (y − y1),
where the slope S is equal to the ratio (x2 −
x1)/(y2 − y1). To draw the
line, for each y from the starting y-coordinate to the ending
y-coordinate, compute the corresponding value x and draw the point
with coordinates (x, y).
Assignment: Part 4. Draw a house -- as in Lab 6 -- using your methods.
Testing: test each of your methods by calling it, from the
main program, at least three times, with different values of
the corresponding parameters. As instructed in class, test each
program on the boundary values of each parameter, and on some
intermediate value (e.g., randomly selected). For example, for a
horizontal line, y-coordinate can be from 0 to 199, so test it for
y = 0, for y = 199, and for some intermediate value y.
When it is due. The program is due at the beginning of the
first lab section on the week of October 21, i.e.:
- on
Monday October 21 for those who attend Monday-Wednesday labs, and
- on Tuesday October 22 for those who attend Tuesday-Thursday
labs.