# 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. Convert the data representation for the US cities from the previous assignment to data definitions in Beginner ProfessorJ language. (Class City)

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

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

4. 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 shapes we may want to draw on the Canvas. We have circles, disks, rectangles, lines, and a combination of shapes - one on the top, another on the bottom.

Here is what you need to know about each of the shapes:

• A circle has the position of the center, the radius, and a color.

• A disk has the position of the center, the radius, and a color.

• A rectangle has the position of the NW corner, the height and the width, as well as a color.

• A line has the position of the start and end points and a color.

• A combo shape consists of the top and the bottom shape.

1. Draw a class diagram of class hierarchy that represent shapes.

2. Define a class CartPt to represent a Cartesian point with integer coordinates.

3. Define the classes that represent shapes. Use the CartPt class to represent the positions of shapes. Use the String type to represent the colors of the shapes.

4. Make examples of data.

## 2.3  Problem

We now modify the shape definitions, and design some methods.

1. Convert the data definitions for the classes that represent shapes so that they use the IColor class hierarchy to represent the colors.

2. Define a method toPosn that produces an instance of the class Posn from this CartPt cartesian point.

3. Design the method centerOf() that produces the CartPt center of this shape as follows:

• for a circle or a disk the center is already given as one of its fields

• the center of the rectangle is halfway between the left and the right edge, and halfway between the top and the botton edge

• the center of the line is halfway between the two ends of the line

• the center of a combo shape if halfway between the centers of the top and the bottom shape

4. Design the method contains that determines whether this shape contains the given CartPt point.

Note: To do this correctly for the class Line requires a bit of geometrical computation. You are allowed to substiute, in this case, a method that produces false for all inputs.

5. Define the method distanceTo that computes the distance between the centers of two shapes.

6. Define the method drawShape that draws this shape in the given Canvas.

## 2.4  Problem

Creative Project

We continue with the design of an interactive game in the style you have done in the first course. 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. Write down the description of the simple version of the game that has been approved by the instructor.

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?

2. Identify the essential information you will need to keep track of as the World scene changes. Design a class to represent this information 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.

6. Design the draw method for each object in the game and for the whole GameWorld. The methods for each object consume the instance of the Canvas on which the object image is to be drawn.