Page 537 - Introduction to Programming with Java: A Problem Solving Approach
P. 537

                4. [after §12.3] Suppose you have three classes—Shape (which defines a shape’s position in a coordinate system), Square (which defines a square’s position in a coordinate system plus the square’s width), and Circle (which defines a circle’s position in a coordinate system plus the circle’s radius). Assume that the three classes form an appropriate inheritance hierarchy with two inheritance relationships. For each of the two inheritance relationships, specify the superclass and subclass.
5. [after §12.3] Suppose you want to create a computer description of various kinds of energy sources, including the four classes: Electrical, EnergySource, Heat, Mechanical, and the six variables: firstCost, fuelUsed, maxRevolutionsPerMinute, maxTemperature, powerOutput, volts. Decide which class should get each variable, establish inheritance relationships, and draw a UML class diagram with class names, variable names, and inheritance arrows. (You may omit type specifications and methods.)
6. [after §12.4] Ellipse program:
Java’s API classes make extensive use of inheritance. For example, Sun’s Java API documentation shows that the java.awt.geom.Ellipse2D package has a class named Double that has these instance variables:3
Exercises 503
 double
  height
The overall height of the Ellipse2D.
  double
  width
The overall width of this Ellipse2D.
   double
 x
Y
The x coordinate of the upper left corner of this Ellipse2D. Apago PDF Enhancer
The y coordinate of the upper left corner of this Ellipse2D.
    double
And it has these constructors:
It has accessors for the instance variables, and an initializing method, but that’s about all. Fortunately, this class extends a class called Ellipse2D, which has several other useful methods, including:
   Double()
Constructs a new Ellipse2D, initialized to location (0, 0) and size (0, 0).
    Double (double x, double y, double w, double h)
Constructs and initializes an Ellipse2D from the specified coordinates.
   boolean
  contains(double x, double y)
Tests if a specified point is inside the boundary of this Ellipse2D.
    boolean
 contains (double x, double y, double w, double h)
Tests if the interior of this Ellipse2D entirely contains the specified rectangular area.
  boolean
  intersects (double x, double y, double w, double h)
Tests if the interior of this Ellipse2D intersects the interior of a specified rectangular area.
   3 These boxed descriptions were copied from Sun’s Java API Web site (http://java.sun.com/javase/6/docs/api/).


































































   535   536   537   538   539