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
Version: 5.2.1

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

A deli menu items include soups, salads, and sandwiches. 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.

Salad also specifies the dressing.

For a sandwich we note the kind of bread, and two fillings (e.g peanut butter and jelly; or ham and cheese). Assume that every sandwich will have two fillings, and ignore extras (mayo, mustard, tomatoes, lettuce, etc.)

Define classes to represent the deli menu and make examples with at least two soups, two salads, and two sandwiches. Start with the class diagrams - it will help you to see the whole design at once.

6 Self-Referential Data

The HtDP book includes the data definition for Ancestor Trees:

;; An Ancestor Tree (AT) is one of

;; -- unknown

;; -- (make-tree Person AT AT)

 

A Person is (make-person String String)

(define-struct (person name eyecolor))

Convert this data definition into Java classes and interfaces. Make examples of ancestor trees that in at least one branch cover at least three generations.

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.