CISC124 Assignment 5: Using interfaces and inheritance
Instructions
Download and save this eclipse project archive file but do not extract it. Copy or move the file into the assignment directory on your computer.
In eclipse, import the project archive using the following steps:
- Under the File menu choose Import…
- Under General choose Existing Projects into Workspace
- Click the radio button beside Select archive file and browse for the zip file
A5_project.zip
- Make sure that the project A5 appears in the list of projects and has a check mark beside it.
- Click Finish to import the project.
Submitting your work
Submit your work on onq under Assignment 4. You should upload two files:
WidthWrapper.java
SpacesWrapper.java
FlushLefttJustifier.java
FlushRightJustifier.java
CenterJustifier.java
DO NOT upload a zip file containing your work. Doing so greatly slows down the processing of assignments, and you will receive a grade of zero on this assignment if you do so.
Your classes should both be in the package a5
(the same package that they appear in in the project file). DO NOT change the package name or remove the package name from your files.
Marking
Marking is based primarily on the correctness of your submitted files. Each file has a grading weight as shown below:
- 35%
WidthWrapper.java
- 35%
SpacesWrapper.java
- 10%
FlushLeftJustifier.java
- 10%
FlushRightJustifier.java
- 10%
CenterJustifier.java
Background information
Wrapping text
Many text-based applications require the ability to wrap text to a fixed column width. Wrapping text involves breaking the text into separate lines so that the length of each line (ideally) is less than some specified maximum width. For example, the following text:
Of course you have a purpose -- to find a purpose.
can be wrapped to lines all having a width of exactly 15 like so (a numbered bar is included below to indicate the column width):
123456789012345
Of course you h
ave a purpose -
- to find a pur
pose.
Alternatively, if the text represents written English, it might be preferable to break the lines at spaces like so:
123456789012345
Of course you
have a purpose
-- to find a
purpose.
Notice that in the above example, each line is as long as possible but no longer than 15 characters wide, and no word is split over two lines.
If the width is too small, then breaking the lines at spaces may not always be possible. In this case, we might choose to break at spaces when we can, and otherwise break in the middle of a word when we have no other choice. For example, wrapping the example text to a width of 5 might produce:
12345
Of
cours
e
you
have
a
purpo
se
--
to
find
a
purpo
se.
In the output above, some lines are shorter than 5 characters because there is a space where the line can be broken at, and some lines are exactly 5 characters long because there are no spaces where the line can be broken at.
Examples of programs that employ word wrapping include:
- eclipse wraps lines of code longer than 80 characters when the user asks eclipse to format their code; the line wrapping strategy employed by eclipse needs to be aware of the programming language so that it does not break lines in locations where a syntax error would occur
- PowerPoint (and other word processing programs) wraps lines of text as you type
- if you enter text in a narrow text box, PowerPoint tries to break lines at spaces if it can; otherwise, it breaks lines at the width of the text box
- web browsers must dynamically wrap lines of text as the size of browser window is changed (for example, when the user rotates their mobile device)
Justifying text
Justification or alignment of text determines how text is aligned to the width of a page or column. Common types of alignment include:
- flush left, or left justified, where the beginning of the text is aligned with the left side of the column,
- flush right, or right justified, where the end of the text is aligned with the right side of the column, and
- center where the text is aligned to the middle of the column
For our purposes, justifying a string involves adding spaces to parts of the string so that the length of the string is equal to the column width.
Justification type | Spaces are added to… |
---|---|
flush left | the end of the string |
flush right | the front of the string |
center | the front and end of the string |
In center justification, an equal number of spaces are added to the front and end of the string if possible; if not, then the end of the string contains one more space than the front of the string.
Consider justifying the string 12345
to a column of width 10. The following table illustrates the justified string using .
to indicate an added space character.
Justification type | Resulting string |
---|---|
flush left | 12345..... |
flush right | .....12345 |
center | ..12345... |
Question
This assignment illustrates the use of inheritance and interfaces to create hierarchy of classes to perform text wrapping and justification. Inheritance is used to create different types of text wrappers and interfaces are used to create different types of text justifiers, but either approach (or a combination of the two) could be used to solve the text wrapping and justification problems.
Text wrapping
Implement the following class hierarchy:
The AbstractStringWrapper
class is already implemented. Study the implementation so that you understand what fields and methods it provides for its subclasses to use. In particular, read the contract for the method wrap()
for instructions on how subclasses are expected to store the wrapped lines of text.
WidthWrapper
The WidthWrapper
class wraps strings so that each line of the wrapped text is exactly equal to the desired maximum width, except for possibly the last line of the wrapped text. It pays no attention to spaces in the string that is being wrapped.
The main
method should output the following:
width:30, lines: 1, lines = [ABCDEFGHIJKLMNOPQRSTUVWXYZ]
width:20, lines: 2, lines = [ABCDEFGHIJKLMNOPQRST, UVWXYZ]
width: 5, lines: 6, lines = [ABCDE, FGHIJ, KLMNO, PQRST, UVWXY, Z]
width: 1, lines:26, lines = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
SpacesWrapper
The SpacesWrapper
class wraps strings by breaking lines so that each line of the wrapped text is as long as possible, up to the desired maximum width, trying to break lines at spaces. If a wrapped line has no spaces in it, then the line is broken at the desired maximum width. If a wrapped line has one or more spaces located before or at the desired maximum width, then the line is broken at the last space before or at the desired maximum width.
The main
method should output the following:
width:30, lines: 1, lines = [ABC DEFGH I JKLMNOPQ RSTUVWXYZ]
width:20, lines: 2, lines = [ABC DEFGH I JKLMNOPQ, RSTUVWXYZ]
width: 5, lines: 7, lines = [ABC, DEFGH, I, JKLMN, OPQ, RSTUV, WXYZ]
width: 1, lines:26, lines = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
Text justification
Study the implementation and documentation of the TextJustifier
interface.
Implement the classes FlushLeftJustifier
, FlushRightJustifier
, and CenterJustifier
.
The String
method repeat
is useful, but not required, when implementing the justifier classes.
Cowsay
The cowsay
program is a Linux program that prints a cow (or other figure) saying some string provided the user. The user of the program can control the width of the printed text; thus, cowsay
requires the ability to wrap text.
The Cowsay.java
program included in the project archive is a simple version of the cowsay
program. It uses a string wrapping class and a string justifier class to wrap and justify a message and then prints the message like so:
Of course you
have a purpose
-- to find a
purpose.
\
\ ^__^
\ (oo)\_______
(__)\\ )\\/\\
||----w |
|| ||
You can edit the message, the wrapped line length, and change the classes that perform the wrapping and justification by editing the main
method of the program after you have completed the assignment question.