©2006 Felleisen, Proulx, et. al.
Abstract over the common fields of Lion
, Snake
, and
Monkey
in the class diagram of figure 5.1. First
revise the class diagram, then define the classes including the
constructors. Solution
+-----------------+ | IZooAnimal | +-----------------+ +-----------------+ / \ | - - - - - - - - - - - - - - - - - - - | | | +-------------+ +------------+ +-------------+ | Lion | | Snake | | Monkey | +-------------+ +------------+ +-------------+ | int meat | | int length | | String food | | String name | | String name| | String name | | int weight | | int weight | | int weight | +-------------+ +------------+ +-------------+
Figure: A class diagram for zoo animals
5.2 Problem (16.3)
Abstract over the common fields in the class diagram of figure 5.2. Revise the class diagram and the classes. Solution
+---------------------+ | ITaxiVehicle | +---------------------+ +---------------------+ / \ | - - - - - - - - - - - - - - - - - - - - | | | +------------------+ +------------------+ +------------------+ | Cab | | Limo | | Van | +------------------+ +------------------+ +------------------+ | | | int minRental | | boolean access | | int idNum | | int idNum | | int idNum | | int passengers | | int passengers | | int passengers | | int pricePerMile | | int pricePerMile | | int pricePerMile | +------------------+ +------------------+ +------------------+
Figure: A class diagram for taxis
5.3 Problem
Develop the method to compute the cost of the ride per passenger, given the number of miles traveled, and assuming the vehicle is filled to its capacity. First, develop the method for the original class hierarchy as shown in figure 5.2. Then, lift the method within the class hierarchy designed in the previous problem and rerun the original tests. Solution
// a set of integers: // contains an integer at most once> class Set { ILin elements; Set(ILin elements) { this.elements = elements; } // add i to this set unless it is already in there Set add(int i) { if (this.in(i)) return this; else return new Set(new Cin(i,this.elements)); } // is i a member of this set? boolean in(int i) { return this.elements.howMany(i) > 0; } } // a bag of integers class Bag { ILin elements; Bag(ILin elements) { this.elements = elements; } // add i to this bag Bag add(int i) { return new Bag(new Cin(i,this.elements)); } // is i a member of this bag? boolean in(int i) { return this.elements.howMany(i) > 0; } // how often is i a member of this bag? int howMany(int i) { return this.elements.howMany(i); } }
Figure: Collections of integers
5.4 Problem (Problem 17.1)
Compare the two classes in figure 5.3. A
Set
is a collection of integers that contains each element at most once. ABag
is also a collecion of integers, but an integer may show up many times in a bag.Your tasks are:
The two class definitions use lists of integers to keep track of elements. Design a representation of lists of integers with this interface:
// a list of integers interface ILin { int howMany(int i); }The constructors are
MTin
for the empty list andCin
for constructing a list from an integer and an existing list.
Develop examples of
Set
s andBag
s.
Develop functional examples for all methods in
Set
andBag
. Turn them into tests.
The two classes clearly share a number of similarities. Create a union and lift the commonalities into an abstract superclass. Name the union interface
ICollection
. Don't forget to re-run your test suite at each step.
Develop the method
size
, which determines how many elements aBag
or aSet
contain. If aBag
contains an integer n times, it contributesn
tosize
.
Develop the method
rem
, which removes a given integer. If aBag
contains an integer more than once, only one of them is removed.Most object-oriented languages provide libraries like
Set
, andBag
. Find the documentation for your local Java implementation and read up onSet
s. Solution
5.5 Problem
Design the game Pong. Pong consists of a ball that is falling down diagonally from left to right and a paddle at the bottom, controlled by the left and right arrow keys. The goal is for the paddle to hit the ball as it gets near the ground.
We will not yet worry about the details of how the game ends, how we keep the score, or what the ball does once it either hits the bottom or is hit by the paddle.
Here is a list of concerns to start with.
Design the class
Ball
to represent the ball in theWorld
. Include the methodsameBall
that can be used to test whether one ball is the same as another ball. Include the method that draws the ball on the given canvas.
Add the method
moveDown
to the classBall
that moves the ball three pixels down and right.
Add the method that determines whether the ball is out of bounds, for a given size of the canvas.
Design the class Paddle to represent the paddle in the game. Include the method
samePaddle
that can be used to test whether one paddle is the same as another paddle. Include the method that draws the paddle on the given canvas.
Add the method movePaddle that consumes a key event (a String) and produces a new paddle moved either left or right by 5 pixels, if left or right key is hit, and in the same place otherwise.
Add the method hitBall that consumes a ball and determines whether the paddle hit the given ball.
Design the class PaddleWorld that extends World and includes a paddle and a ball. To do so, you must design the methods draw, erase, onKeyEvent and onTick.
Design the method makeBall that produces a new ball at a random height on the left side of the canvas.
The method draw should draw the paddle and the ball on a black background, the method erase should just draw the background.
The onKeyEvent should move the paddle, as specified in the movePaddle method, leaving the ball at the same place.
The onTick method should move the ball down until it either is hit by the paddle or goes out of bounds. When that happens a new ball comes into play.
Run the world.
5.6 Writing Problem
Writing assignments are separate from the rest of the assignment for the week. You should work on this assignment alone,and submit your work individually on the due date for the rest of the homework. The answer should be about two paragraphs long -- not to exceed half a page or 300 words.
There is a huge number of programming languages available to programmers today. Investigate why are new languages invented, how are they different from each other, why are some better than other, or more popular. Support your findings with references to at least two sources you have read. Solution
Last modified: Monday, February 6th, 2006 11:08:14pmHTML conversion by TeX2page 20050501