Java Reference
In-Depth Information
a) Parent for Callback Test window
b) Child One window
c) Child Two window
FIGURE 10-64
7 Constructing Shapes through Inheritance
and Aggregation
Using the files, TestCircle.java, Circle.java, and Shape.java, from the Chapter 10
folder on the Data Disk, modify the code from the completed Debugging
Assignment to use inheritance, an interface, and aggregation by completing
the following steps:
1. In the Shape class, eliminate the concrete methods, area() and perimeter();
create an instance variable of type String called name; and make a
constructor that requires a String, shapeName, as the name of the new
Shape object. Keep the method getLocation(), and keep the Shape class
abstract.
2. Create an interface, Shape2D, which requires the methods, area() and
perimeter(), which were eliminated from Shape.
3. Create a new class, Point, which inherits from Shape and contains two
private instance variables of type int, x and y, which store the coordinates
for the Point. Point should have both a default constructor and a constructor
that takes two int values for the coordinates x and y. For the default
constructor, set both x and y to zero.
4. Modify the class, Circle, to inherit from Shape as well as use the interface,
Shape2D. Circle should have only two private instance variables: a double
named, radius, and a Point object reference named, center. Circle should
have two constructors, a default constructor and a constructor that takes a
double as a new radius value and two ints as the x and y coordinates of the
center. Circle does not store the x and y values as ints, but instead creates a
Point object with these coordinates. The location of a Circle is the location
of its center, Point.
Search WWH ::




Custom Search