Instructions:Omitted
Getting Ready:Omitted
Document
and
FormattedDocument
classes. (Note: These
classes are different from any earlier versions you may have
seen or used.)
Driver1
.
This document has 18 words and 2 lines
George is a little monkey, and all monkeys are curious.
But no monkey is as curious as George.
Driver2
.
This document has 18 words and 2 lines
George is a little
monkey, and all
monkeys are
curious.
But no
monkey is as curious
as George.
getLineCount()
method.)
getLineCount()
method uses the
text
attribute directly rather than calling the
getText()
method.
text
in the Document
class private.
Document
and
FormattedDocument
classes (in that order).
FormattedDocument.java:50: text has private access in Document
tokenizer = new StringTokenizer(text, " ");
^
text
). (Hint: How can a FormattedDocument
object get access to text
if it's private?)
getText()
method rather
than the text
attribute. That is:
tokenizer = new StringTokenizer(super.getText(), " ");
Driver2
. (Note: You should not
get a runtime exception. If you do, you made a mistake when you
answered the previous question that resulted in an infinite
recursion - a method calling itself forever. You should be able
to figure it out; if not, ask for help before proceeding.)
This document has 18 words and 2 lines
George is a little
monkey, and all
monkeys are
curious.
But no
monkey is as curious
as George.
Driver2
, the getDescription()
message is
sent to the object named formatted
.
What code is executed as a result (i.e., what class
is the code in)?
Document
class. (Note: The
getDescription()
method is inherited.)
getDescription()
method sends the message getLineCount()
to
this
. What code is executed as a result? Why?
this
refers to the object named formatted
in Driver2
. Hence, the
FormattedDocument
class is searched first.
But, since the FormattedDocument
class does not have
an explicit getLineCount()
method, the parent class is
searched (for an inherited method). There is such a method in the
Document
class so the code in that class is executed.
getLineCount()
method in the
Document
to the following:
getText()
method (and a
local variable) rather than the text
attribute.
Driver2
, the getText()
message is
sent to the object named formatted
.
What code is executed as a result? Why?
FormattedDocument
class (because the
method in the FormattedDocument
class overrides
the method in the Document
class).
Driver2
, the getDescription()
message is
sent to the object named formatted
.
What code is executed as a result (i.e., what class
is the code in)?
Document
class.
getDescription()
method sends the message getLineCount()
to
this
. What code is executed?
this
refers to the object named formatted
in Driver2
. Hence, the
FormattedDocument
class is searched first. But the
FormattedDocument
class does not have an explicit
getLineCount()
method. Hence, the parent class is
searched (for an inherited method). There is such a method in the
Document
class so the code in the Document
class is executed.
getLineCount()
sends the getText()
message to this
. What code is executed as a result?
Why?
getLineCount()
message is sent to the object named formatted
in Driver2
which means that this
refers
to formatted
. Hence, the FormattedDocument
class is searched first. There is a getText()
method in the FormattedDocument
class (it overrides the
version in the Document
class) so the code in the
FormattedDocument
class is executed.
Driver2
.
This document has 18 words and 7 lines
George is a little
monkey, and all
monkeys are
curious.
But no
monkey is as curious
as George.
Driver1
.
This document has 18 words and 2 lines
George is a little monkey, and all monkeys are curious.
But no monkey is as curious as George.
Driver3
.
Driver3
?
print()
methods, one that is passed a
Document
and one that is passed a
FormattedDocument
.
Driver3
.
A nicely formatted document:
George is a little
monkey, and all
monkeys are
curious.
But no
monkey is as curious
as George.
print()
method was executed?
FormattedDocument
.
print()
method executed?
print()
in
main()
, it needed to look for a
print()
method that can be passed the object named
formatted
. Since formatted
is declared to
be a FormattedDocument
object it found two, one that is
passed a Document
and one that is passed a
FormattedDocument
. Both are appropriate because
formatted
is polymorphic (i.e., it is both a
Document
and a FormattedDocument
. The
compiler chooses the most specialized/specific version which, in this
case, is the version that is passed a FormattedDocument
.
Driver3
, comment-out the print()
method that is passed a
FormattedDocument
.
Driver3
compile? Why or why not?
FormattedDocument
"is a"
Document
(i.e., FormattedDocument
specializes
Document
).
Driver3
.
A document:
George is a little
monkey, and all
monkeys are
curious.
But no
monkey is as curious
as George.
print()
method was executed?
Document
.
print()
method executed?
print()
method.
print()
method that is passed a
FormattedDocument
. (That is, make sure Driver3
again has both print()
methods.)
Driver3
, modify the declaration of
formatted
so that it is now a Document
.
Document formatted;
Driver3
compile? Why or why not?
FormattedDocument
"is a"
Document
(i.e., FormattedDocument
specializes
Document
).
Driver3
.
A document:
George is a little
monkey, and all
monkeys are
curious.
But no
monkey is as curious
as George.
print()
method was executed?
Document
.
print()
method executed?
print()
in
main()
, it needed to look for a
print()
method that can be passed the object named
formatted
. Since formatted
is declared to
be a Document
object it found only one, the one that is
passed a Document
.
Even though, at run time, formatted
will actually be a
FormattedDocument
object, the compiler can't always know
that. So, it uses the declared type.
getText()
message is sent to the object named
doc
at run-time. At run time, the doc
object is
a FormattedDocument
. So, the FormattedDocument
class is searched first.
Driver3
,
comment-out the print()
method that is passed a
Document
.
Driver3
.
Driver3.java:25: print(FormattedDocument) in Driver3 cannot be applied to (Docum
ent)
print(formatted);
^
1 error
print()
in
main()
, it needed to look for a
print()
method that can be passed the object named
formatted
. Since formatted
is declared to
be a Document
object it didn't find one.
Copyright 2011