ISSEP 2011 Workshop --- Program by Design: ReachJava (How to Design Classes)

Overview

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 return and 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 example classes Circle and Rectangle that 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.


Workshop materials (references): Level 3 (ReachJava)

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.


Workshop materials (software, tutorials, exercises): Level 3 (ReachJava)

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:


Getting started

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.


First Programs --- Exploration

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:


Working with Complex Data

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 just a String to represent the color.)

Here are some examples:

Simple mobile:   Complex mobile:

        |                   |
        |               ----+------
        20              |         |
       blue            30         |
                       red     ---+---
                               |     |
                              10    10
                             green  red

Designing World Programs

Creat a project named WorldProject. Import into it the three files that use the draw library:

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.


Food for Thought

To explore further the ideas presented in this workshop, look at the following programs.