Solutions to 14 Chapter Exercises Inheritance and Polymorphism Consider the following class definitions. Identify invalid statements. 14.1. class Car { public String make; protected int weight; private String color; ... } class ElectricCar extends Car { private int rechargeHour; public ElectricCar( ) { ... } //copy constructor public ElectricCar ( ElectricCar car ) { this.make = car.make; this.weight = car.weight; this.color = new String( car.color ); 1
2 Solutions to Chapter 14 Exercises this.rechargeHour= car.rechargeHour; } ... } class TestMain { public static void main ( String args[] ) { Car myCar; ElectricCar myElecCar; myCar = new Car(); myCar.make = "Chevy"; myCar.weight = 1000; myCar.color = "Red"; myElecCar = new ElectricCar(); myCar.make = "Chevy"; myCar.weight = 500; myCar.color = "Silver"; } } 1. The ElectricCar constructor cannot refer to this.color or car.color be- cause color is a private member of the superclass Car . 2. In the main method of the TestMain class, it is illegal to make refer- ence to myCar.color since color is a private data member of the Car class. 3. The reference myCar.weight in the TestMain class main method is in- valid because the weight data member is declared protected. Consider the following class definitions. Identify which calls to the con- 14.2. structor are invalid. class Car { public String make; protected int weight; private String color;
Solutions to Chapter 14 Exercises 3 private Car ( String make, int weight, String color ) { this.make = make; this.weight = weight; this.color = color; } public Car ( ) { this( "unknown", -1, "white" ); } class ElectricCar extends Car { private int rechargeHour; public ElectricCar( ) { this( 10 ); } private ElectricCar(int charge ) { super( ); rechargeHour = charge; } } class TestMain { public static void main ( String args[] ) { Car myCar1, myCar2; ElectricCar myElec1, myElec2; myCar1 = new Car(); myCar2 = new Car("Ford", 1200, "Green"); myElec1 = new ElectricCar( ); myElec2 = new ElectricCar( 15 ); } } In the main method of the TestMain class, invoking the three-argument constructor of Car or the one-argument constructor of ElectricCar are illegal because these constructors were declared private in their respec- tive classes.
4 Solutions to Chapter 14 Exercises In the ComputeGrades sample program, we set the default size of the 14.3. roster array to 25. Modify the program so the size of the array will be increased if the input file contains more than 25 students. You need to add a method that expands the array, say, by 50 percent. Extend the ComputeGrades sample program by storing the roster array 14.4. using ObjectOutputStream . To allow the user to create and read the data using any text editor, add the menu choices Import and Export , which will read in a textfile (this is how the original ComputeGrades works) and write out data in ASCII format to a textfile. Extend the ComputeGrades sample program to include menu choices 14.5. Save , Open , and Quit . With the current program, you can open one file for each execution of the program. Extend the program so the user can open more than one file by selecting the menu choice Open repeatedly. Selecting the menu choice Save will allow the user to save the comput- ed results to a file he or she specifies. How would you modify the ComputeGrades sample program if the for- 14.6. mula for computing the course grade is different for freshmen, sopho- more, junior, and senior undergraduate students? Would you design four subclasses of UndergraduateStudent ? Or would you modify the body of the computeCourseGrade method of UndergraduateStudent ? Discuss the pros and cons of each approach. We should not create four new subclasses of UndergraduateStudent . We can be reasonably sure that no new type of undergraduate will come into existence, so it is unlikely that this method will need to change in the future. If we choose instead to create four new subclasses, we have a new set of problems. First, we need to differentiate among the four classes when a new student enrolls so that we create an appropriate in- stance. Second and more important, we have a natural progression each year as freshmen become sophomores, sophomores become juniors, and juniors become seniors. If we create four subclasses, then at the end of each school year, we need to convert all of the freshmen objects into sophomore objects and so on. This is a large amount of overhead. With the single subclass UndergraduateStudent , we use an instance variable to indicate the class to which a student belongs to (perhaps the numbers 1-4 for freshman-senior), at the end of each school year we simply up- date this instance variable (perhaps by adding one) to indicate the stu- dent has progressed to the next level. Write a personal finance manager program that maintains information 14.7. on your bank accounts. Incorporate the following rules:
Solutions to Chapter 14 Exercises 5 • For the savings accounts, you can make a maximum of three withdrawals in a month without incurring a fee. The bank charges $1.00 for every withdrawal after the third. • For the checking accounts, the bank charges $0.50 for every check you write for the first 20 checks (i.e., withdrawals) in a month. After that, there will be no charge. You should be able to open and save account information to a file. You should be able to list all transactions of a given account or of all ac- counts. Include appropriate menus to select the options supported by the program. Consider using the Date class to record the date of transac- tions. The Date class is from the java.util package. Please refer to a ja- va.util reference manual for information on this class. Extend the address book sample program from Chapter 9. Instead of 14.8. managing a single type of Person , incorporate additional types of per- sons such as PersonalFriend , BusinessAssociate , etc. Define these classes as a subclass of Person . Design carefully to decide whether the Person class will be an abstract class or not. Consider an asset tracking program that will track four types of assets: 14.9. electronic appliances, automobiles, furniture, and compact discs. What classes would you design for the program? Would you define four unre- lated classes or one superclass and four subclasses? If you design a su- perclass, will it be an abstract superclass? An abstract superclass Asset with four subclasses Appliance , Automo- bile , Furniture , and CompactDisc is a good design. Any number of dif- ferent asset types may be added in the future by subclassing Asset and implementing any abstract methods it declares. Implement the asset tracking program of exercise 9. Allow the user to 14.10. add, modify, and delete electronic appliances, automobiles, furniture, and compact discs. Allow the user to list the assets by category and search for an asset by its serial number. Extend the asset tracking program of exerci se10 by adding an object I/ 14.11. O capability. Write an application that reads daily temperatures for 12 months and al- 14.12. lows the user to get statistics. Support at least three options: monthly av- erage of a given month, yearly average, and lowest and highest temperatures of a given month. Use a textfile to store temperatures. A line in the textfile contains daily temperatures for one month. The first line in the textfile contains temperatures for January, the second line for
6 Solutions to Chapter 14 Exercises February, and so forth. Use StringTokenizer to parse a line into tempera- tures of type float . For a data structure, consider using either an array of Month or a 2-D array of float . Month is a class you define yourself.
Recommend
More recommend