Page 538 - Introduction to Programming with Java: A Problem Solving Approach
P. 538
504
Chapter 12 Aggregation, Composition, and Inheritance
Write a short program in a class called EllipseDriver:
Import java.awt.geom.Ellipse2D.Double, and write a main method that calls the
4-parameter Double constructor to instantiate an ellipse like that shown in the picture below. Then,
in println statements, call the superclass’s 2-parameter contains method to show whether the points x3.5, y2.5 and x4.0, y3.0 are contained within the specified ellipse.
4
3.5 4.0
3.0
4.0
2.5 3.0
2.0
7.
contains x=3.5, y=2.5? false
contains x=4.0, y=3.0? true
[after §12.5] Define a class named Circle that is derived from this API superclass: java.awt.geom.Ellipse2D.Double
8.
See Exercise 6 for a brief description of this Double superclass. Your subclass should declare the
two private instance variables, xCtr and yCtr, initialized to 0.0. These variables are the x- and y-coordinates of the circle’s center. Your class should include a zero-parameter constructor, and it should also include a 3-parameter constructor whose parameters are the x- and y-distances to the circle’s center and the circle’s diameter. This 3-parameter constructor should not only initialize the new instance variables but also use the 4-parameter constructor of the superclass to initialize the four instance variables in the superclass. Your class should also provide the following accessor methods: getXCtr, getYCtr, and getRadius, where the radius is half the height of the superclass shape. Verify the code you write by compiling it and running it with representative values for x-center, y-center, and diameter.
[after §12.6] Suppose you have two classes related by inheritance that both contain a zero-parameter method named doIt. Here’s the subclass’s version of doIt:
public void doIt()
{
System.out.println("In subclass's doIt method.");
doIt();
} // end doIt
5.0
Output:
Apago PDF Enhancer
4 The program does not actually draw the ellipse, but the ...Ellipse2D.Double class has a mathematical understanding of it.