Lab 2: Designing classes of data: unions, lists.


Overview of the design recipe for classes

Problem analysis.

Analyze the problem statement carefully to determine with what kind of information your program must cope. Find classes of information in your problem statement, not specific pieces of information, and give some examples to supplement the description of your classes.

Class diagrams (data definition)

Now translate the informal class descriptions into a detailed class diagram that shows all the relationships between classes and interfaces. To do that, you have to recognize five situations and translate them into boxes and arrows.
1. Use a primitive type if there is a correspondence between the information and an atomic type that the language supports.
2. Use a class when you want to represent information that consists of several other pieces of information, e.g. a date.
3. If a field of a class A is itself a class (say B) then class A refers to class B. When you create an instance of A it contains an instance of B. When a class refers to another class it is called compound class, when it doesn't it is called primitive.
4. When you have a collection of distinct classes and you want to give them a common header use a union of classes.
5. If you have a piece of information that consists of an arbitrary number of other pieces then the class diagram is self-referential.

Class definitions, purpose statements

Now, the class diagram can be turned into class definitions automatically. Each class is equipped with a constructor. Containment arrows disappear and inheritance arrows become implements specifications. Add a purpose statement to each class to explain what information the class represents.

Examples.

Translate your informal examples into objects. By this you verify that your class hierarchy can represent the information of the problem statement. Also, make up examples of objects and interpret them in the problem domain.


Unions of classes, using containment

You are a game designer and you have to implement a minesweeper game. The following Scheme code represents the mines in a mine-field.
#|
a Minefield is one of:
- empty
- (cons Mine Minefield)
|#

(define-struct mine(row col))
;a mine is a structure: (make-mine Number Number)
;row and col define the position of a mine in the minefield

(define mine1 (make-mine 1 4))
(define mine2 (make-mine 12 11))
(define mine3 (make-mine 7 21))

(define mines (cons mine1 (cons mine2 (cons mine3 empty))))
- Design a class diagram for the previous data definitions.
- Represent these data definitions as Java classes.
- Translate the Scheme examples into instances of Java classses.
- Make two additional examples of mines and one more example of a minefield.
- Draw a game grid (on paper) that shows the positions of the mines in your second example (you are free to choose the dimensions of the grid).

Quiz


More complicated class hierarchies

You are asked to design the software to keep track of sporting events. At this time the organizers run competitions in three different sports: swimming, bicycling, and running. The swimmers compete in four different styles (freestyle, breast stroke, back stroke, and butterfly), with several distances in each style, and, of course, males and females compete separately. Runners compete in track (either sprints or hurdles) and in cross-country runs. Again, there are several different distances and separate events for males and females. Finally, the bicycling has two kinds of competitions - on a track in a stadium, or on the roads. Again, there are several different distances and separate competitions for males and females.

- Write down Scheme-like data definitions for the class of data that represents a Sport.
- Design the class diagrams on a piece of paper.
- Make up three examples for each kind of sports.
- Define Java classes that correspond to your class diagram.
- Translate your examples into Java code.

A competition consists of an arbitrary number of sport-events. To represent a sport-event we need a sport and three winners (name and time for each).

- Extend your previous class diagram to include Sport-event and Competition.
- Modify your Java code (and examples) accordingly.

Homework submission details

INCLUDE INSTRUCTIONS FOR SETTING UP HOMEWORK SUBMISSION HERE...