Lab
12 : Experimenting with Polymorphism
Advanced
Programming - CS 239 Department of Computer Science
Getting Ready: Before going any further you should:
1. Make a directory on your N: drive for
this lab.
2. Setup your development environment.
3. Download the file named Document.java
4. Download the file named FormattedDocument.java
5. Download the file named Driver1.java
6. Download the file named Driver2.java
7. Make sure you understand
the classes you just copied. (Note: These classes are different from any
earlier versions you may have seen or used.)
Part I: This part of the lab will help you learn how to find
errors related to polymorphism.
8. Compile all of the classes and execute Driver1. Is
the output correct?
|
9a. Execute Driver2. What is wrong with the output?
|
9b. Why is the output incorrect? (Hint: Add output
statements that will help you debug the getLineCount() method.)
|
Part II: This part of the lab reviews some issues involving
accessibility/visibility.
10a. Make the attribute, text,
in the Document class private. Re-compile the Document and FormattedDocument
classes (in that order). What compile-time errors are generated
|
10b. Why were they generated?
|
11a. Fix the problem (without changing the
accessibility/visibility of text). (Hint: How can a FormattedDocument object
get access to text if it's private?)
|
11b. What did you do to fix the problem?
|
12. Compile and execute Driver2. Is the output
correct?
|
Part II: This part of the lab will help you gain a better
understanding of polymorphism.
13. When the
getLineCount() message is sent to the object named, formatted, what code is executed?
|
14. Replace
the getLineCount() method with the
contents of getLineCount.java and explain the changes .
|
15. When the getLineCount() message is sent to the
object named formatted, what does this
refer to?
|
16. Continuing with this same example, when the
getText() message is sent to this, what code is
executed?
|
17. Compile and run the driver. Is the output
correct?
|
18. Compile and run the
original driver (i.e., the version that uses a Document object). Is the output
correct?
|