©2010 Felleisen, Proulx, et. al.

2  Designing Classes

Portfolio Problems

Work out as complete programs the following exercises from the textbook:

Problems:

  1. Problem 2.4 on page 17

  2. Problem 3.1 on page 25

  3. Problem 4.4 on page 33

  4. Problem 5.3 on page 44

  5. Problem 5.9 on page 51

  6. Problem 10.2 on page 97

  7. Problem 10.5 on page 105

  8. Problem 14.1 on page 140

  9. Problem 14.7 on page 144

Pair Programming Assignment

2.1  Problem

  1. Define data representation for the US cities in FunJava language. The file caps.txt provides the desired information for the capitals of the 48 contiguous states. (Class City)

  2. Make sure you have a separate class Loc for the location given in the latitude and longitude coordinates.

  3. Make examples of the capitals of the six New England states.

  4. Define the class hierarchy that represent a list of cities. Follow the DESIGN RECIPE FOR DATA DEFINITIONS. Remember to make examples of data.

  5. Define the class hierarchy that represents that represent a list of states identified by a String (typically two letters — the same format as is used in the City class. Follow the DESIGN RECIPE FOR DATA DEFINITIONS. Remember to make examples of data.

2.2  Problem

Design the classes to represent a lunch order. The customer can choose a soup (soups have different flavors), a salad (one of several kinds, as well as add one of several choices of dressings), and a sandwich. For the sandwich the customer selects the bread, the filling, and a list of extras (tomatoes, onions, mayo, mustard, olives, etc.) For each of the lunch parts we care whether they are vegetarian. The restaurant charges individually for each selection, but all breads cost the same and all extras cost the same. There is no extra charge for the salad dressing.

Make examples of at least two of each: soup, salad, dressings, breads, fillings, extras, and at least four examples of lunches, at least one of them a vegetarian choice.

The class that represents the entire lunch order should be named Lunch. You can name all the other classes as you wish, but make sure your names are sensible, and the class names start with a capital letter.

2.3  Problem

Creative Project

Once again (you have done so in Fundies 1) we ask you to design of a graphics-based interactive game. A game consist of several different objects. The object move either on each tick of the clock, or in response to the keys (typically the arrow keys). There may be other changes in the game object over the time or in response to the key events (x key launches a shot, an animal gets hungrier as the time goes on, ...). The objects interact in some predefined manner. Finally, something (the state of an object, the interaction between objects) triggers the end of the game.

  1. Think of a game you would like to build, keeping in mind the limitations and functionality described above.

  2. For each object that will be used in the game do the following:

    1. Describe briefly its behavior during the game: does it change with the clock tick?, does in respond to key events?, does it interact with another object in the game?

      Write this as a paragraph of text, for example: A fish swims from left to right, ...

    2. Identify the essential information you will need to keep track of as the World scene changes. Design the classes to represent the objects in the game and make examples of data, especially those at the beginning of the game, or in any expected unusual situations during the game.

    3. Design the class GameWorld that includes all objects involved in the game.

    4. Make examples of the initial GameWorld and a couple of intermediate worlds you expect to see in the game.

    5. Design a picture that will represent each of the objects in your game. A picture is composed of disks, circles (outlines), rectangles, lines and text in six possible colors: red, blue, green yellow, white, or black. Draw on a paper a picture that represents your world at some point during the game.

Last modified: Thursday, September 16th, 2010 3:29:30pm