1 | Open your last version of the DoME project. Remove the print() method from class Item and move it into the Video and CD classes. Compile. What do you observe? | There is a compilation error since the title field belongs to the Item class from which we moved the method. |
2 | In your DoME project, add a print() method in class Item again. For now write the method body with a single statement that prints out only the title. Then modify the print() methods in CD and Video so that the CD version prints out only the artist and the Video version prints only the director. This removes the other errors ecountered above. You should now have a situation coressponding to figure 9.4 in the text, with print() methods in three classes. Compile your project. This design should work, if there are errors remove them. Before executing, predict which of the print() methods will get called if you execute the Database list() method. Try it out: Enter a CD and a video into the database and call the Database list() method. Which print() methods were executed? Was your prediction correct? Try to explain your observations. | Prediction: When executing the database print method, it will use the specific CD and Video print methods which will print out only either the artist or director, since we've overloaded the method in the sub-classes. My prediction was correct. |
3 | Modify your latest version of the DoME project to include the super call in the print() method. Test it. Does it behave as expected? Do you see any problems with this solution? | Yes it works, no problems: |
4 | Change the format of the output so that it prints the string "CD: " or Video: " (depending on the type of item) in front of the details. | Done: |
Monday, 8 November 2010
CS Chapter 9: 9.1-9.4
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment