Java Reference
In-Depth Information
public void addLine(String line)
to add a line of text to the body of the letter.
Supply a method
public String getText()
that returns the entire text of the letter. The text has the form:
Dear recipient name:
blank line
first line of the body
second line of the body
. . .
last line of the body
blank line
Sincerely,
blank line
sender name
Also supply a program LetterPrinter that prints this letter.
Dear John:
I am sorry we must part.
I wish you all the best.
Sincerely,
Mary
Construct an object of the Letter class and call addLine twice.
Hints: (1) Use the concat method to form a longer string from two shorter
strings. (2) The special string "\n" represents a new line. For example,
the statement
body = body.concat("Sincerely,").concat("\n");
adds a line containing the string ȒSincerelyȓ to the body.
΢΢Exercise P3.9. Write a class Bug that models a bug moving along a
horizontal line. The bug moves either to the right or left. Initially, the bug
moves to the right, but it can turn to change its direction. In each move, its
position changes by one unit in the current direction. Provide a constructor
Search WWH ::




Custom Search