Algorithms and OOD (CSC 207 2014F) : Labs
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [Learning Outcomes] [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Student-Curated Resources] [Java 8 API] [Java 8 Tutorials] [Code Conventions]
Related Courses: [CSC 152 2006S (Rebelsky)] [CSC 207 2014S (Rebelsky)] [CSC 207 2014F (Walker)] [CSC 207 2011S (Weinman)]
Misc: [Submit Questions] - [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.
a. Fork and clone the repository at
https://github.com/Grinnell-CSC207/lab-polymorphism.
b. Import that repository into Eclipse.
a. Scan through MathUtils.java and verify that
the square root method has the form described in the reading.
b. Run the main method of MathExpt to see
that it behaves as expected.
c. Add lines to the main method of MathExpt
so that it computes the square root of
an Integer,
a Float,
a Double,
a BigInteger,
a BigDecimal, and
a double.
As a first step in understanding the layout methods, add code to the
main method of TBExpt.java so that it
uses TBUtils.print to print a simple block. I
would suggest something like the following.
TextBlock block = new TextLine("This is a test.");
TBUtils.print(pen,block);
The reading
claims that it is possible to combine text blocks together by
using one as a parameter to the constructor of another. (In fact,
that seems to be the only way to create a BoxedBlock.)
Try creating and printing out each of the following:
The classes HComposition and VComposition permit you
to compose pairs of text blocks horizontally and vertically. For
example, new HComposition(tb1,tb2) makes a text block by
putting tb1 and tb2 side-by-side, and
new VComposition(tb1,tb2) makes a text block by putting
tb1 on top of tb2.
a. Using VComposition, TextLine, and
BoxedBlock, build the following text block:
+-------+ |Hello | |Goodbye| +-------+
b. Using VComposition, TextLine, and
BoxedBlock, build the following text block:
+-----+ |Hello| +-----+ +-------+ |Goodbye| +-------+
c. Using HComposition, TextLine, and
BoxedBlock, build the following block:
+-----+Goodbye |Hello| +-----+
d. Using HComposition, TextLine, and
BoxedBlock, build the following block:
Goodbye+-----+
|Hello|
+-----+
Assume that HComposition 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 HComposition.
b. Compare your answer to the source code of HComposition.
Assume that VComposition has two fields,
TextBlock top; TextBlock bottom;
a. Sketch how you might write
String row(int rownum), int width(), and int height().
You can look at the source code of BoxedBlock and
HComposition for ideas, but please do not look at the source
code of VComposition.
b. Compare your answer to the source code of VComposition.
If you find that you have extra time, do one or both of the following.
Use the various implementations of TextBlock to make an
“interesting” textual composition.
Develop a new class that, like BoxedBlock or
HComposition, represents a new block based on
one or more previously defined blocks.
Thanks to LBB and TD for pointing out typos.