Advanced Programming - CS239
Department of Computer Science
LAB 11:
EXPERIMENTING WITH ACCESSIBILITY/VISIBILITY
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 Document.java from this directory
4. Download the file FormattedDocument.java
from this
directory
5. Download the file Driver.java
from this
directory
Lab 11 – Accessibility and
Visibility Deliverable
Name _________________
Do steps 1..5
and put the files on your your flash
drives or floppies in addition to your N drive
then PAUSE and STUDY the classes you have just
created. As part of your studying,
annotate the classes with comments that tell what you think is significant
about each method. Continue to do this as you make the changes described in
this lab.
Answer the
following questions:
1.What is the
relationship between FormattedDocument and Document? FormattedDocument is a child of Document |
2.Who has
access to the attributes of Document? only a Document itself
and the methods in the class |
3.What is
happening in FormattedDocument in the line:
super(text); The explicit value constructor of Document is being called |
Part 1:
1. What output
do you expect will occur when you compile all the classes and execute the
driver? (answer this BEFORE compiling
and running the driver).
Contains 18 word(s) George is a little monkey, and all monkeys are
curious. But no monkey is as curious
as George. |
What output
did you get? ÏϧÏContains
18 word(s). |
Does this
match your answer above? yes |
If not, why
not? |
2. Don't change the
declaration of the variable named doc but change the line
containing
doc = new Document(text); to doc = new String(text);. Re-compile the driver.
What error was
generated? ÏDriver.java:24:
incompatible types |
3. Don't change the
declaration of the variable named doc but change the line that now
contains doc = new String(text); to doc = new FormattedDocument(text,
20);.
4. Re-compile the driver. Why
did it compile even though there appear to be
incompatible types?
because a FormattedDocument is an extension (child) of Document
and a FormattedDocument is-a Document |
5. Re-execute the driver.
Show the output below.
ÏThis document has 18 words and at
least 4 lines. |
Did it output what you
expected? yes |
If so, why? If not, why not? |
6. The getText() method in the FormattedDocument class contains the line temp =
super.getText();. Explain this line of code.
the FormattedDocument object
is calling the getText() method of its parent (the Document) |
7. Replace the line temp = super.getText(); with the line temp = getText(). Recompile this class and
re-execute the driver. What happened and why?
program bombs because the FormattedDocument object calls its own
getText() method recursively until there is no more room on the stack. |
Part II: In this part of the lab you
will experiment with changing the
accessibility/visibility of attributes and methods.
1. Make the accessibility of
the getDelimiters() method in the Document class
private. Re-compile only the Document class. What happens and why?
compiles correctly |
2. Re-compile only the FormattedDocument class. What happens and why?
doesn’t compile because the getDelimiters() method in the document
class is private so it can’t be accessed by its child FormattedDocument. |
3. Now make the getDelimiters() method protected and re-compile all of the
classes. What happens and
why?
compiles correctly because
a child has access to the protected methods of its parents |
4. What's the difference
between the public version and the protected version?
everyone has access to the public version only objects of the class itself and its children have access to
the protected version |
Which is better?
opinion - no points for this
one |
Why?
opinion - no points for this
one |
5. Perform the same
experiments with the getWordCount()method.
Which version is better?
same results opinion - no points for this one |
Why?
opinion – no points for this
one |
6. Change the accessibility
of the delimiters and text attributes in the Document class to protected. What
changes can you now make to the FormattedDocument
class?
(Hint: Think about how the FormattedDocument class accesses these attributes.)
you can just call
super.delimiters and super.text (i.e. access the attributes directly without
needing a method to get their values) |
7. Do you like these changes?
Why or why not?
opinion – no points |
8. Now that the delimiters attribute is protected, do you still need the
getDelimiters() method?
NO! |
9. Should either the getDescription() or getText() mehods in the Document class be protected?
opinion – no points |
Why or why not?
opinion – no points |