Algorithms and OOD (CSC 207 2013F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Summary: In today's laboratory, you will explore issues of polymorphism in Java.
Equipment
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.
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Java 7 API] [Java Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2013S (Walker)] [CSC 207 2011S (Weinman)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)] [Issue Tracker (Textbook)]
Copyright (c) 2013 Samuel A. Rebelsky.
This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this
license, visit http://creativecommons.org/licenses/by/3.0/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.