[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
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.
Contents:
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 toString
method.
1. Implement each of the following textual composition classes:
Truncate
which builds (from a textblock and a specified
width) a new textblock that truncates the underlying textblock to the
desired width.
Center
which builds (from a textblock and a specified
width) a new textblock that centers the underlying textblock within
the specified width.
RightJustify
which 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 TextLine
.
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 toString
is
generally expected to return a single-line string. Let's explore how
we might do so.
a. TextLine
's toString
could simply return the line.
b. BoxedBlock
's toString
could return the line surrounded by square braces, as in "[Hello]"
.
c. HCompose
's toString
could return the two parts surround by parentheses and separated by a plus, as in "(Hello+Goodbye)"
.
d. VCompose
's toString
could return the two parts surround by parentheses and separated by a slash, as in "(Hello/Goodbye)"
.
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 toString
methods.
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.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
Disclaimer:
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 http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2005F/Homework/hw.03.html
.
You may wish to
validate this document's HTML
;
;
Check with Bobby