Espresso: A Concentrated Introduction to Java
Summary: In today's laboratory, you will explore issues of polymorphism in Java.
Equipment
Contents
In this laboratory, you will use two new packages entitled
username.math
and
username.layout
.
a. In a terminal window, type
/home/rebelsky/bin/espresso math /home/rebelsky/bin/espresso layout
You should see messages about files being copied to a temporary directory.
b. Start Eclipse.
c. In Eclipse, build a project named Temp from
/home/username/CSC152/Temp
.
d. In the Temp project, you should see packages named
username.math
and
usernamelayout
. Drag those packages to your
Code project.
e. Delete the Temp project.
You can now work with the new packages.
a. Scan through Mathematician.java
and verify that it has the form
described in the reading.
b. Extend the main
method of TestMath
so that
it computes the square root of
an Integer
,
a Float
,
a Double
,
a BigInteger
, and
a BigDecimal
.
c. Verify that the square roots are correct by printing out the squares of the computed roots.
As a first step in understanding the layout methods, extend the
main
method of username.layout.Tester
so that it
uses TBUtils.print
to print a simple block. I
would suggest something like
TextBlock block = new TextLine("This is a test."); TBUtils.print(pen,block);
In the reading, I claimed that it was possible to combine text blocks together. Try creating a boxed phrase and a boxed boxed phrase.
The classes HCompose
and VCompose
permit you
to compose pairs of text blocks horizontally and vertically. For
example, new HCompose(tb1,tb2)
makes a text block by
putting tb1
and tb2
side-by-side, and
new VCompose(tb1,tb2)
makes a text block by putting
tb1
on top of tb2
.
a. Using VCompose
, TextLine
, and
BoxedBlock
, build the following text block:
+-------+ |Hello | |Goodbye| +-------+
b. Using VCompose
, TextLine
, and
BoxedBlock
, build the following text block:
+-----+ |Hello| +-----+ +-------+ |Goodbye| +-------+
c. Using HCompose
, TextLine
, and
BoxedBlock
, build the following block:
+-----+Goodbye |Hello| +-----+
d. Using HCompose
, TextLine
, and
BoxedBlock
, build the following block:
Goodbye+-----+ |Hello| +-----+
Use the various implementations of TextBlock to make an interesting
textual composition.
Assume that HCompose
has two fields,
TextBlock left; TextBlock right;
a. Sketch how you might write
String row(int rownum)
,
int width()
, and
int height()
.
You can look at the source code of BoxedBlock
for ideas,
but please do not look at the source code of HCompose
.
b. Compare your answer to the source code of HCompose
.
Assume that VCompose
has two fields,
TextBlock top; TextBlock bottom;
a. Sketch how you might write
String row(int rownum)
,
int width()
, and
You can look at the source code of BoxedBlock
and
HCompose
for ideas, but please do not look at the source
code of VCompose
.
b. Compare your answer to the source code of VCompose
.
Pick one of the following composition mechanisms and implement it as a class.
a. Truncate
, which, given a textblock and a maximum width,
truncates the block to that width.
b. Center
, which, given a textblock and a width, centers
the block within that width.
c. RightJustify
, which, given a textblock and a width,
right-justifies the block within that width.
Spring 2005 [Samuel A. Rebelsky]
Tuesday, 27 September 2005 [Samuel A. Rebelsky]
Wednesday, 28 September 2005 [Samuel A. Rebelsky]
Tuesday, 28 February 2006 [Samuel A. Rebelsky]
This page was generated by
Siteweaver on Thu Mar 30 15:24:40 2006.
The source to the page was last modified on Tue Feb 28 09:49:08 2006.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Espresso/Labs/polymorphism.html
.
You may wish to
validate this page's HTML
;
;
Check with Bobby