THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

name. Modify the main program so that it generates the same output it did before. 54


Exercise 2.8: What constructors should you add to LinkedList? 54


Exercise 2.9: Add a static method to Vehicle that returns the highest identification number used thus far. 58


Exercise 2.10: Add a toString method to Vehicle. 60


Exercise 2.11: Add a toString method to LinkedList. 60


Exercise 2.12: Considering your Vehicle and LinkedList classes, can you think of a need for any
methods that take a variable number of arguments? 61


Exercise 2.13: Make the fields in your Vehicle class private, and add accessor methods for the fields.
Which fields should have methods to change them, and which should not? 68


Exercise 2.14: Make the fields in your LinkedList class private, and add accessor methods for the
fields. Which fields should have methods to change them, and which should not? 68


Exercise 2.15: Add a changeSpeed method that changes the current speed of the vehicle to a passed-in
value and add a stop method that sets the speed to zero. 68


Exercise 2.16: Add a method to LinkedList to return the number of elements in a list. 68


Exercise 2.17: Add two turn methods to Vehicle: one that takes a number of degrees to turn and one that
takes either of the constants Vehicle.TURN_LEFT or Vehicle.TURN_RIGHT. 71


Exercise 2.18: Change Vehicle.main to create cars with owners whose names are specified on the
command line, and then print them. 74


Exercise 3.1: Starting with the Vehicle class from the exercises in Chapter 2, create an extended class
called PassengerVehicle to add a capability for counting the number of seats available in the car and the
number currently occupied. Provide a new main method in PassengerVehicle to create a few of these
objects and print them out. 79


Exercise 3.2: Type in the classes X and Y as shown previously, and add print statements to trace the values of
the masks. Add a main method and run it to see the results. (Hint: Use the printf methodshown in Chapter
1with a format specifier of %x to print integers in hexadecimal format.) 83


Exercise 3.3: If it were critical to set up these masks using the values from the extended class during
construction, how could you work around these problems? 83


Exercise 3.4: Which methods (if any) of Vehicle and PassengerVehicle might reasonably be made
final? 97


Exercise 3.5: Write a new extended class that benchmarks something else, such as how long it takes to run a
loop from zero to some passed-in parameter. 99


Exercise 3.6: Change Vehicle so that it has an EnergySource object reference, which is associated with
the Vehicle in its constructor. EnergySource must be an abstract class, because a GasTank object's
measure of fullness will differ from that of a Battery object. Put an abstractempty method in
EnergySource and implement it in GasTank and Battery classes. Add a start method to Vehicle
that ensures that the energy source isn't empty. 99

Free download pdf