1 Introduction
2 Eclipse IDE
Learn to set up your workspace.
The First Project
Set up the run configuration and run the program.
Simple Data Definitions
Data Definitions with Containment
Data Definitions for Unions of Data
Self-Referential Data
5.3.5

Lab 1

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.

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 #tt{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) developmnet environment used by many professional Jav programmers (as well as programmers the 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:

Learn to set up your workspace.

The First Project

Add examples of data needed to display the following image (ignore the colors):

Set up the run configuration and run the program.

Simple Data Definitions

Problem 1

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 "Pat" 19 "F"))

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

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

Draw the class diagram that represnts 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.

Data Definitions with Containment

Problem 2

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.

Data Definitions for Unions of Data

Problem 3

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.

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