This part of the curriculum shows that the lessons from the first two parts extend naturally to the design of programs in the setting of a class based language using object-oriented paradigm.
We start with designing data - designing classes of data that are connected to each other in a systematic way, showing that the Design recipe for Data Definitions can be used virually without change in a completely different language than we have used in the first part.
We start designing methods in the functional style, i.e., every
method produces a new value (primitive or a new object) and makes no
changes in the existing data. This allows us to use a very simple
language, one where the only two statements are
if (condition) then statement else statement.
The programs we provide give you examples of the progressively more complex data (class) definitions, and illustrate the design of methods for these class Hierarchies.
The design of methods follows
the same Design Recipe we have seen before. The only
difference here is that for classes that represent a union type (for
are both a part of the union type
Shape, the conditional
statement used in teachScheme! inventory is replaced by the
dynamic dispatch into the class whose constructor has been used to
define the specific object.
The draft of the textbook for the Level 3 (second semester, Java-based) How to Design Classes
The home page for the tester library.
The home page for the javalib libraries for the design of interactive graphics-based games.
The home page for the Program by Design program contains links to other materials as well as information about workshops, mailing lists, and other ways that help you learn more about this curriculum.
For this workshop we have prepared copies of all materials you may need.
All materials have been installed on the lab computers. However, if you want to use the materials on your own laptop or at home, please use the links shown above, as well as the following link.
EclipseJars.zip The libraries for the Level 3 (second semester, Java-based) of the workshop.
Programs.zip The zipped file with the programs we will work on.
Individual files for the Java libraries:
tester.jar The tester library for testing Java programs.
colors.jar The library for assigning colors to drawing shapes.
The library that represents a
draw.jar The library for drawing shapes and designing interactive games in a functional style.
idraw.jar The library for drawing shapes and designing interactive games in an imperative style.
isdraw.jar The library for drawing shapes and designing interactive games with sound and music in an imperative stype.
tunes.jar The library for generating sound and music --- in a standalone mode, or in conjunction with interactive games.
Copy the folder EclipseJars in the H directory, into your local D directory. These are the libraries your programs will use.
Copy the folder EclipseWorkspace in the H directory, into your local D directory. This is where you will do all your work.
If you have Eclipse installed on your laptop and wish to work there, start with a new workspace, copy the sources needed from the Programs.zip link, copy the libraries needed from the EclipseJars.zip link, unzip the file and ask for help in setting up the projects and importing the needed files.
Start the Eclipse application and select the new EclipseWorkspace you have created.
Create a project StartingUp. Import into it the files
Book1.java, Book2.java, Book3.java, Book4.java, Book5.java,
Book6.java, Book7.java Book8.java, Book9.java. These programs
show the progressive introduction of classes, interfaces, unions of
classes, and classes with self-reference, as well as the use of the
Design Recipe for designing methods for these classes. Read
the code, add to it new methods or examples of data, just to get
familiar with the ideas. Create a Run Configuration for each
file by selecting in the Run menu the Run
Configurations... tab, clicking on the New icon in the
Run the programs by selecting in the to left corner, naming the
configuration by the name of the file, and choosing the corresponding
Examples class as the class that contains the
main method. Run each configuration and observe the results.
The nine programs cover the following topics:
Book1.java A simple definition of one class with examples of data and a drawing of the class diagram.
Book2.java An example of designing methods for a simple class, including the template and tests.
Book3.java A class definition with a field that is an instance of another class (reference).
Book4.java A class definition for a union of classes (i.e. classes that implement a common interface), together with the examples of designing methods for these classes. Here the students first see the power of the dynamic dispatch.
Book5.java A class definition with a union (of a list with no items and a list with at least one first item) that represents a recursively defined list, together with examples of data.
Book6.java Designing methods for self-referential data (lists). An example of the power of following the shape of the data definitions.
Book7.java The conversion of the example of designing
methods for unions (Book4) into
one that uses an
abstract class to eliminate the
Book8.java The motovation for designing and using function objects -- classes that implement an interface that represents functional behavior, so this can be passed to another function.
Book9.java An example that uses function objects to eliminate the repetition in method definitions in the previous example.
Create the project MobilesProject. Import into it the file Mobiles.java. The classes defined there represent mobiles, hanging from the ceiling. We can describe mobiles as follows:
A mobile is either a simple ball (we know its color and
weight) hanging on a string of some length, or it is a string of
some length that has at the end a horizontal strut (we know the
length of the left and right part from the place where it is
suspended) with a mobile hanging from each end of the strut. (Use
String to represent the color.)
Here are some examples:
Simple mobile: Complex mobile: | | | ----+------ 20 | | blue 30 | red ---+--- | | 10 10 green red
Make sure you understand the data definitions. Draw another
mobile, then make example of the mobile in the
Design the following methods for your mobiles:
Finish the design of the method that computes the total weight of this mobile. Ignore the weight of the struts and the lines on which the weights hang.
Design a method that will produce the total height of this mobile. Each ball has height 10.
Design a method that will produce a new mobile where every red ball is replaced with a yellow one.
Design the method that determines whether the mobile is balanced. The mobile is balanced when the weight of the left mobile multiplied by the length of the left strut equals the weight of the right mobile multiplied by the length of the right strut, and every mobile hanging from each strut is also balanced.
Creat a project named WorldProject. Import into it the three files that use the draw library:
A program that
just draws a line and a small black ball, and illustrates how to
Canvas and the drawing within.
Run the program, read the code, maybe add another shape to the display.
A program that
shows how to design the methods that implement the
World to play a simple interactive graphics-based
The program starts four copies of the world
(initially they all are shown on top of each other, but you can move
the frames apart, so you can watch the interactions with each of
them). The ball moves at random, responds to the arrow keys by
moving in the specified direction, responds to the three letters
R, Y, G and changes the color of the ball to red,
yellow, green, and responds to the mouse click by moving the
blob to the mouse position (making it red again).
Run the program, look at the code, add new response to a key press (e.g. on key W the color changes to white.
A program that illustrates the design of a more complex game, with several interacting objects (a hungry shark and the fish the shark wishes to catch).
Study the design - which class is responsible for checking whether a fish has been found by the shark, whether the shark has been fed, etc.
Try to design a simple game yourself. For example, a UFO is falling down, the anti-UFO platform on the bottom moves left and right, and fires shots when the space bar is hit. If one of the shots hits the UFO, the player wins the game, if UFO lands on the Earth, the player looses.
To explore further the ideas presented in this workshop, look at the following programs.
Import the file BooksAuthorsCircles.zip The files for the lecture on circularly referential data and designing the equality comparison for this kind of data.
ExamplesBooksAuthors class contains a lecture on
the design on circularly referential data, on the design of methods
that compare complex objects for equality. The additional classes
illustrate the whole idea.
This is a fairly old program that shows how difficult the test-first design would be without the support of our tester library.
This program illustrates the richness of the programs students can design using this approach. The solution is available upron request.
For this problem you will use the classes that represent the
values in the cells of a spreadsheet. For each cell we record the
row and column where the cell is located, and the data stored in
that cell. The data can either be a numerical (integer) value or a
formula. Each formula can be one of three possible functions:
+ (representing addition),
mn (producing the
minimum of the two cells), or
* (computing the product)
and involves two other cells in the computation.
| A | B | C | D | E | ---+----------+----------+----------+----------+-----------+ 1 | 8 | 3 | 4 | 6 | 2 | ---+----------+----------+----------+----------+-----------+ 2 | mn A1 E1 | + B1 C1 | | | * B2 D1 | ---+----------+----------+----------+----------+-----------+ 3 | * A1 A2 | + B2 B1 | | | mn A3 D1 | ---+----------+----------+----------+----------+----------+ 4 | | + B3 B2 | | | mn B4 D1 | ---+----------+----------+----------+----------+-----------+ 5 | | + B4 B3 | | | * B5 E4 | ---+----------+----------+----------+----------+-----------+
Make sure you design templates, use helper methods, and follow the containment and inheritance arrows in the diagram.
This program contains only the data definitions for representing a binary search tree. Use it to design the typical methods one would design for the binary search trees.
Think of how you would modify the definition to represent a tree that contains arbitrary types of data, and where the user can choose the ordering that the search tree represents.