Java Reference
In-Depth Information
protected AbstractDrawFunction () {
drawFunction();
}
/** Return the y-coordinate */
abstract double f( double x);
/** Obtain points for x-coordinates 100, 101, . . ., 300 */
public void drawFunction() {
for ( int x = -100 ; x <= 100 ; x++) {
p.addPoint(x + 200 , 200 - ( int )f(x));
}
}
@Override /** Draw axes, labels, and connect points */
protected void paintComponent(Graphics g) {
// To be completed by you
}
}
Test the class with the following functions:
f(x) = x 2 ;
a.
b.
f(x) = sin(x);
c.
f(x) = cos(x);
d.
f(x) = tan(x);
e.
f(x) = cos(x) + 5sin(x);
f.
f(x) = 5cos(x) + sin(x);
f(x) = log(x) + x 2 ;
g.
For each function, create a class that extends the AbstractDrawFunction
class and implements the f method. Figure 15.9 displays the drawings for the
first three functions.
F IGURE 15.9
Exercise 15.1 draws the square, sine, and cosine functions.
**15.2
( Triangle class ) Design a new Triangle class that extends the abstract
GeometricObject class. Draw the UML diagram for the classes Triangle
and GeometricObject and then implement the Triangle class. Write a test
program that prompts the user to enter three sides of the triangle, a color, and a
Boolean value to indicate whether the triangle is filled. The program should cre-
ate a Triangle object with these sides and set the color and filled properties
using the input. The program should display the area, perimeter, color, and true
or false to indicate whether it is filled or not.
 
Search WWH ::




Custom Search