1 Introduction
2 Eclipse IDE
2.1 Learn to set up your workspace.
2.2 The First Project
2.3 Set up the run configuration and run the program.
3 Simple Data Definitions
4 Data Definitions with Containment
5 Data Definitions for Unions of Data
6 Self-Referential Data
7 The First Methods

Lab 2

Goals: The goals of this lab are to get familiar with our work environment: the Eclipse IDE, the WebCAT submission, the basics of running a program in Java-like languages, and program testing framework.

In the second part of the lab, (the one that really teaches you something) will focus on data definitions and examples in a simple variant of Java language.

1 Introduction

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 expr and

if predicate 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 DrRacket inventory/template is replaced by the dynamic dispatch into the class whose constructor has been used to define the specific object.

2 Eclipse IDE

Eclipse is an integrated (program) development environment used by many professional Java programmers (as well as programmers who use other programming languages). It is an Open Source product, which means anyone can use it freely and anyone can contribute to its development.

The environment provides an editor, allows you to organize your work into several files that together comprise a project, and has a compiler so you can run your programs. Several projects form a workspace. You can probably keep all the work till the end of the semester in one workspace, with one project for each programming problem or a lab problem.

There are several step in getting started:

2.1 Learn to set up your workspace.

2.2 The First Project

Add examples of data needed to display the following image (ignore the colors). The size of the canvas (and the blue background) is 100 x 100.

2.3 Set up the run configuration and run the program.

3 Simple Data Definitions

Here is a data definition in DrRacket:

;; to represent a person

;; A Person is (make-person String Number String)

(define-struct (person name age gender))


(define tim (make-person "Tim" 20 "M"))

(define pat (make-person "Ann" 19 "F"))

(define pat (make-person "Pat" 19 "F"))

(define kim (make-person "Kim" 17 "F"))

(define dan (make-person "Dan" 22 "M"))

Draw the class diagram that represents this data.

Define the class Person that implements this data definition and the class ExamplesPerson that contains the examples defined above.

Run your program to make sure it works.

4 Data Definitions with Containment

Modify your data definitions so that for each person we also record the person’s address. For person’s address we only record the city and the state.

5 Data Definitions for Unions of Data

The Soup and Salad Deli menu items include soups, salads, and breads. Every item has a name and a price (in cents - so we have whole numbers only).

For each soup and salad we note whether it is vegetarian or not. The customer can select either a cup or a bowl of soup.

Salad also specifies the dressing.

Breads can be gluten free or not.

Define classes to represent the deli menu and make sufficient number of examples so that you will order at least two different soups, two different salads, and two different breads. Start with the class diagrams - it will help you to see the whole design at once.

6 Self-Referential Data

The youth soccer coach need to notify all players if the game is cancelled due to rain, and so she set up a phone chain. The coach calls the team captain who starts the phone chain by calling two other players on the team. Every player who receives the call then calls two other players (or one or none, if the player is at the end of the phone chain). This continues until all players have been called.

Here is the phone chain for the Astros team:


         /             \

       May            Bea

    /      \        /     \

  Kim     Pat     Ann     Joy

 /   \   /   \   /   \   /   \

Tay Zoe Meg Lou Cam Eve Tam  EMPTY

/ \ / \ / \ / \ / \ / \ / \

;; An Phone Chain (PC) is one of

;; -- EMPTY

;; -- (make-link Player PC PC)


A Player is (make-player String String)

(define-struct (player name phone-num))

Convert this data definition into Java classes and interfaces. Make examples of several phone chains including the representation of the phone chain shown above.

7 The First Methods

For the classes that define a person with an address, design the method with the following header and purpose statement:

// is this person from the same city as the given person?

boolean sameCity(Person other)

Make sure you follow the rule one task – one method and design each method in the class that should be responsible for it.