Lab 1: Classes, Diagrams and Definitions


Setup

Open lab1.java in DrScheme (you can copy/paste the code into a new window, or right-click the link, "Save as...", and "Open" from DrScheme). lab1.java contains Java code you will use in this lab. This is also where you will write your new Java code. When you need to write Scheme code, open a new DrScheme window to do so.

You can set your language level using the "Language" menu, choosing "Language...", then selecting your language. In this lab, use "Beginning Student" under "HtDP" for Scheme work and "Beginner" under "ProfessorJ" for Java work.


Review of HtDP Struct Definitions

First, open DrScheme and make sure your language level is set to "Beginning Student" (See the "Language" menu, "Choose Language..." option, under the HtDP section).

As a review, here is an example of a data definition for structured data as presented in HtDP, dealing with data for shapes. Enter the following code into a the definitions section of a new DrScheme window.

(define-struct rect (height width))
;; A Rect is a (make-rect Number Number)

;; interpretation: (make-rect h w) represents a rectangle with height h
;; inches, and width w inches.

Try making some example rectangles. Enter the following into the interactive section of your DrScheme window. Note: the ">" means you should type the line at the interactive prompt, you should not type the ">".

> (define rect-square (make-rect 3 3))
> (define rect-wide (make-rect 4 10))
> (define rect-tall (make-rect 12 5))

Class Diagrams

We will now convert the above data definition for rectangles into a class diagram.

A basic class diagram represents a class as a box containing the class name at the top, followed by a description of each data element (i.e. field) in the class, including the corresponding data types. The class diagram below corresponds to the definition of Rect (see also lab1.java).

+-----------------------+
| Rect                  |
+-----------------------+
| int height            |
| int width             |
+-----------------------+

The data type int is a representation of integers built into Java. Some of the most frequently used Java data types are:

Java Keyword Description Examples
boolean true or false true, false
int integer number 3, 42
double floating point number 3.14159, 42.0
char charachter 'a', 'b', '!'
String string of characters "PI", "3", "Forty-two"

The first four data types above are primitive types in Java. That is, they are "built in", and don't need a corresponding class definition to describe their behavior. String, on the other hand, is defined by the String class in Java (the String class is always included and doesn't need to be defined by the programmer). Classes defined by the programmer create new non-primitive data types.


Java Classes Definitions

We now convert the data definition for Rect into a Java class definition. Below is the Java class definition for Rect (see also lab1.java).

class Rect {
	int height; //height in inches
	int width;  //width in inches

	Rect(int height, int width) {
		this.height = height;
		this.width = width;
	}
}

The first line above defines a class named Rect. All of the code between the following { ... } represents the definition of that class. At the top of the class is a definition of each of the data members (or fields) of that class, in this case the ints height and width. This is followed by a Java constructor that defines how to create a new instance of a class. The constructor takes initial values for the fields as arguments and produces an instance of the class with fields that have the specified values.

There is also another class in lab1.java, Examples. This class is used to test other classes by defining a number of instances of that class. In this case there are three examples of the Rect class, rectSquare, rectTall, and rectWide. When we create an instance of Examples, its fields will be the specific instances of the class Rect.

Try each of the following lines in DrScheme (in the lab1.java window) and observe the results.

> Examples e = new Examples();
> e.rectSquare
> e.rectTall

The first line above defines a new instance of Examples names e. The keyword new signifies that we are calling a constructor method to create a new instance.


Exercise 1

Design another data definition for a shape, this time for a circle that has a radius and a color. Do each of the four steps that we used above. Note: specify the color of the circle using the Java type String, as in "blue" or "green".

  1. Define the struct in Scheme.
  2. Draw the class diagram (either by hand or using ASCII art).
  3. Define the Java class in lab1.java.
  4. Add three examples of your circle class to the Examples class.

Structs with Containment

We now address defining data with containment. In Scheme, this means a struct where one or more fields are also structs. The equivalent in Java is a class with one or more fields for which the data type is a class, not a primitive type. Note: String is a special class predefined in Java. Fields of type String are not considered to represent a containment relationship.

In the above examples, we described some general shapes. But, if we want to draw those shapes on a screen, we also need to know the location of that shape. Let's modify the Rect class to include its location, defined by the number of pixels its top-left corner is away from the top-left corner of the screen, in both the vertical and horizontal dimensions. We could add these two numbers directly to the Rect class, but we may want to know the locations of other shapes as well. To make the definition of locations reusable, we'll make location a data type.

Below is a Scheme definition of Loc, a location. Also, we modify the Rect struct to include a location.

(define-struct loc (vert horiz))
;; A Loc is a (make-loc Number Number)

;; interpretation: (make-loc h v) represents a location v pixels down and
;; h pixels left of the top-left corner of the screen

(define-struct rect (loc height width))
;; A Rect is a (make-rect Loc Number Number)

;; interpretation: (make-rect l h w) represents a rectangle with its top-left corner at location l,
;; height h pixels, and width w pixels

Some examples to try:

> (define loc1 (make-loc 10 30))
> (define loc2 (make-loc 42 42))

> (define rect-square (make-rect loc1 3 3))
> (define rect-wide (make-rect loc2 4 10))
> (define rect-tall (make-rect (make-loc 123 55) 12 5))

Class Diagrams with Containment

Containment in class diagrams is shown by an arrow from the field representing a data element defined by another class to that class. For example, the class diagram for our new definitions of Loc and Rect is:
+-----------------------+
| Rect                  |
+-----------------------+
| Loc loc               |---+
| int height            |   |
| int width             |   |
+-----------------------+   |
                            |
+-----------------------+   |
| Loc                   |<--+
+-----------------------+
| int vert              |
| int horiz             |
+-----------------------+

Java Classes with Containment

The following are the new Java class definitions for Loc and Rect (see also lab1.java). Notice the use of the newly defined class name, Loc, as the data type of the new field in Rect.

// A class representing locations
class Loc {
	int vert;  //pixels down from the top-left corner of the screen
	int horiz; //pixels left from the top-left corern of the screen

	Loc(int vert, int horiz) {
		this.vert = vert;
		this.horiz = horiz;
	}
}

// Another class representing rectangles, this time with a location
class Rect2 {
	Loc loc;    //location of upper-left corner
	int height; //height in pixels
	int width;  //width in pixels

	Rect2(Loc loc, int height, int width) {
		this.loc = loc;
		this.height = height;
		this.width = width;
	}
}

Also review the examples of instances of these classes in Examples. Try the following and make sure the results are what you expect.

> Examples e = new Examples();
> e.loc1
> e.rectSquare2

Exercise 2

Add a location to the circle you defined in Exercise 1. Follow each of the steps we did above:

  1. Modify the Scheme struct.
  2. Draw the class diagram (either by hand or using "ASCII art").
  3. Modify the Java class in lab1.java.
  4. Add three examples of your circle class using locations to the Examples class and examine them interactively.

Pair-up and Set-up Homework Submission Account

Find a partner. You will work with your partner on all labs and homework assignments. In labs, you should designate one partner as the "driver", who types, and one as the "co-pilot", who thinks and suggests corrections and improvements to the code. You must switch roles half-way through the lab, so each partner experiences each role. Note: this also means you're working on the same computer, not just at adjacent computers.

INCLUDE INSTRUCTIONS FOR SETTING UP HOMEWORK SUBMISSION HERE...