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

                502
Chapter 12 Aggregation, Composition, and Inheritance
10. 11.
If a subclass method overrides a method in the superclass, is it still possible to call the method in the superclass from the subclass?
If a superclass declares a variable to be private, can you access it directly from a subclass?
§12.7 Using the Person/Employee/FullTime Hierarchy
12. If you wish to call a superclass method, you must always prefix the method name with super. (T / F)
§12.8 The final Access Modifier
13. A final method is called “final” because it’s allowed to contain only named constants, not regular
variables. (T / F)
§12.9 Using Inheritance with Aggregation and Composition
14. Composition and inheritance are alternative programming techniques for representing what is essentially
the same kind of real-world relationship. (T / F).
§12.10 Design Practice with Card Game Example
15. A Deck is a group of cards and a Deck has a group of cards. In our example, it’s better to choose the
is-a relationship and implement inheritance. In this case, why is inheritance a better choice than composition?
§12.11 Problem Solving with Association Classes (Optional)
16. It’s possible to support an association with references, variables, and methods in existing classes. What’s the
advantage of using an association class instead?
Exercises
Apago PDF Enhancer
 1. [after §12.2] (This exercise should be used in combination with Exercises 2 and 3.) Write a definition for a Point class. Provide two double instance variables, x and y. Provide a two-parameter constructor that initializes x and y. Provide a shiftRight method that shifts the point in the x direction by the value of the method’s double parameter, shiftAmount. Provide a shiftUp method that shifts the point in the y direction by the value of the method’s double parameter, shiftAmount. Make each of these methods return values that enable chaining. Provide accessor methods to retrieve the values of the two instance variables.
2. [after §12.2] (This exercise should be used in conjunction with Exercise 1 and 3.) Write a definition for
a Rectangle class. Provide two Point instance variables, topLeft and bottomRight, which establish the top left and bottom right corners of the rectangle, respectively. Provide a two-parameter constructor that initializes topLeft and bottomRight. Provide a shiftRight method that shifts the rectangle in the x direction by the value of the method’s double parameter, shiftAmount. Provide a shiftUp method that shifts the rectangle in the y direction by the value of the method’s double parameter, shiftAmount. Make each of these methods return values that enable chaining. Provide a printCenter methodthatdisplaysthexandyvaluesofthecenteroftherectangle.
3. [after §12.2] (This exercise should be used in conjunction with Exercise 1 and 2.) Write a definition
for a RectangleDriver class with a main method to do the following: Instantiate a Point called topLeft at x 􏰀 􏰁3.0 and y 􏰀 1.0. Instantiate a Point called bottomRight at x 􏰀 3.0 and y 􏰀 􏰁1.0. Instantiate a Rectangle called rectangle using topLeft and bottomRight as arguments. Call rectangle’s printCenter method. Use a single chained statement to shift the rectangle right by one and then up by one. Call rectangle’s printCenter method again. The output should be:
x = 0.0 y = 0.0
x = 1.0 y = 1.0







































































   534   535   536   537   538