2  Monday Afternoon:

2.1  Goals

Introduction to the design recipe for class-based data definitions: class definition, class diagram, the constructor, examples of the instances of data.

  1. simple classes

  2. classes with containment

  3. unions of classes

2.2  Example: Definition of the class Posn

/*
+-------+
| Posn  |
+-------+
| int x |
| int y |
+-------+
*/

// to represent one point on  a canvas
class Posn {
  int x;
  int y;

  Posn(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

/* Interactions:
Examples of constructing instances of Posn
 
Posn p1 = new Posn(10, 20);
Posn p2 = new Posn(20, 20);

Examples of the use of the field selectors for the class Posn

p1.x  -- expected: 10
p1.y  -- expected: 20
p2.x  -- expected: 20
p2.y  -- expected: 20
*/