Assigned: Friday, 30 September 2005
Due: 11:00 a.m., Wednesday, 5 October 2005
Summary: In this assignment, you will further explore the polymorphic text composition presented in the reading and lab on polymorphism.
Purpose: To enhance your understanding of polymorphism and of object-oriented design.
In the reading and lab on polymorphism, you explored a number of related classes that permitted you to build interesting two-dimensional layouts of characters. You were able to combine and recombine text objects because of Java's support of polymorphism. That is, if we treat every combination of text as an object that implements TextBlock (and that is likely to be constructed from other TextBlocks), then we can combine these objects arbitrarily.
The textual layout example also had a hidden agenda: To help you think about problems in a more object-oriented fashion. In particular, most beginning programmers, when asked to do some form of textual composition, focus on the methods that would compose pieces of text. In this example, we built objects that composed pieces of text. The advantages of using objects included the ability to reuse a composed piece of text and a better ability to take advantage of polymorphism.
However, we left some aspects of the example unexplored in
both the reading and the lab. In particular, few of you had
the opportunity to build your own composition classes, we did
not consider possible effects of mutating textblocks, and we
did not write the traditional
1. Implement each of the following textual composition classes:
Truncatewhich builds (from a textblock and a specified width) a new textblock that truncates the underlying textblock to the desired width.
Centerwhich builds (from a textblock and a specified width) a new textblock that centers the underlying textblock within the specified width.
RightJustifywhich builds (from a textblock and a specified width) a new textblock that right-justifies the underlying textblock within the specified width.
2. The basic building block of text compositions is the
Suppose we added a
setContents(String newline) method to that
class which changes the underlying line.
a. What changes would we have to make to the other composition classes to support that change?
b. Make those changes (and indicate within the code that you have made them).
3. As some of you noted, although we think of TextBlocks as representing
text, they do not provide a
toString method. Part of the
problem with providing such a method is that
generally expected to return a single-line string. Let's explore how
we might do so.
toString could simply return the line.
toString could return the line surrounded by square braces, as in
toString could return the two parts surround by parentheses and separated by a plus, as in
toString could return the two parts surround by parentheses and separated by a slash, as in
e. You may, but need not, design similar ways to represent the additional text composition mechanisms described in part 1 of this assignment.
Implement all of these
Create a directory for the package, share the directory and its contents, and send me the location of that directory.
You should work in groups of two or three on this assignment.
When you ask questions, I'll try to put the answers here.
Friday, 30 September 2005 [Samuel A. Rebelsky]
I usually create these pages
on the fly, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Dec 6 09:47:03 2005.
The source to the document was last modified on Fri Sep 30 09:29:51 2005.
This document may be found at