Instructions: Answer the following questions one at a time. After answering each question, check your answer (by clicking on the check-mark icon if it is available) before proceeding to the next question.
You may work on this assignment alone or in a group.
You must be checked-off at the end of the lab period. Otherwise, you must submit a hardcopy of the worksheet before 5:00PM today.
Getting Ready: Before going any further, you should:
downloads
directory/folder). In most browsers/OSs, the
easiest way to do this is by right-clicking/control-clicking on
each of the links above and then selecting
.java
Files: In most IDEs, .java
files
(i.e., source files) can just be copied into the project.
See the course "Help" page on your IDE for more information.
.class
and .jar
Files:
In some IDEs it is easier to use .class
files and in others it is easier to use a .jar
file
that contains the .class
files. Hence, you have been
provided with both.
See the course "Help" page on your IDE for more information.
Resources: In some IDEs, resources (e.g., images, data files) need to be in a special directory whereas in others they need to be in the working directory. See the course "Help" page on your IDE for more information.
Document
and
FormattedDocument
classes. (Note: These
classes are different from any earlier versions you may have
seen or used.)
Driver1
.
Driver2
.
getLineCount()
method.)
text
in the Document
class private.
Document
and
FormattedDocument
classes (in that order).
text
). (Hint: How can a FormattedDocument
object get access to text
if it's private?)
Driver2
. (Note: You should not
get a run-time 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.)
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)? Why?
getDescription()
method sends the message getLineCount()
to
this
. What code is executed as a result (i.e., what class
is the code in)? Why?
getLineCount()
method in the
Document
to the following:
public int getLineCount() { char character; int count, i; String temp; temp = this.getText(); // Initialize the line counter count = 1; if (temp.length() == 0) count = 0; // No words means no lines // Count the number of newline characters for (i=0; i < temp.length(); i++) { character = temp.charAt(i); if (character == '\n') count = count + 1; } return count; }
Driver2
, the getText()
message is sent to the object named formatted
. What
code is executed as a result (i.e., what class is the code in)?
Why?
getText()
method in
the FormattedDocument
class and
the getText()
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)?
getDescription()
method sends the message getLineCount()
to
this
. What code is executed?
getLineCount()
sends the getText()
message to this
. What code is executed as a result?
Why?
Driver2
.
Driver1
.
Driver5
.
Driver5
.
Driver5
? Why?
println()
to a.getText() == b.getText()
.
Driver5
.
Driver5
? Why?
println()
to a.equals(b)
.
Driver5
.
Document
class does not have an implementation
of an equals()
method. Why does the class compile?
Driver5
.
Driver5
?
equals()
method so that a Document
object can be correctly compared to another Document
object.
(Reminder: Don't use an if
statement!)
Document
class?
FormattedDocument
class?
println()
to a.toString()
.
Driver5
.
Document
class does not have an implementation
of a toString()
method. Why does the class compile?
Driver5
.
Driver5
?
toString()
method so that it
returns the result of a call to getText()
.
Document
class?
Driver5
.
Driver5
?
Driver5
so that a
is
instantiated as a FormattedDocument
(with a width of 20).
Driver5
.
Driver5
?
toString()
message is sent to a
,
what method is executed? Why?
getText()
message is sent to this
(inside of the toString()
method),
what method is executed? Why?
Driver3
.
Driver3
?
Driver3
.
print()
method was executed (i.e., what is the signature
of the method that was executed)?
print()
method executed?
Driver3
, comment-out the print()
method that is passed a
FormattedDocument
.
Driver3
compile? Why or why not?
Driver3
.
print()
method was executed?
print()
method executed?
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
.
Driver3
compile? Why or why not?
Driver3
.
print()
method was executed?
print()
method executed?
Driver3
,
comment-out the print()
method that is passed a
Document
.
Driver3
.
Driver4
.
Driver4
?
Driver4
.
print()
method was executed in iteration 0?
print()
method executed?
print()
method was executed in iteration 1?
print()
method executed?
documents
from
Document[]
to FormattedDocument[]
and compile Driver4.java
. What error messages were generated?
Why?
documents
back
to Document[]
and the allocation of memory
for documents
from
Document[]
to FormattedDocument[]
.
Will Driver4.java
compile? Hint: Think about one line
at a time.
Driver4
. What error message is generated? Why?
Driver6
.
Driver6
?
Driver6
.
print()
method was executed (i.e., what is the signature
of the method that was executed)?
print()
method executed?
print()
method that is passed a
FormattedDocument
.
Driver6
.
print()
method was executed (i.e., what is the signature
of the method that was executed)?
print()
method executed?
Copyright 2022