©2007 Felleisen, Proulx, et. al.
Problem 15.10 on page 175 in the text dealt with shopping lists.
Design the method that produces a shopping list sorted by the
brand names. The class String
defines the following method:
// this String is lexicographically before that String: positive result // this String is same as that String: result is 0 // this String is lexicographically after that String: negative result int compareTo(String that);
Rewrite the methods howMany
, brandList
, and
highestPrice
using accumulator style.
Problem 4.5 asked you to design data definitions for files of images,
text, and sounds. Problem 14.6 asked you to add methods
timeToDownload
, smallerThan
, and sameName
.
Design the appropriate abstract class that eliminates as much repetition in the code as possible. Solution
Problem 18.3. Solution
In problem 14.7 you designed methods unitPrice
,
lowerUnitPrice
, and cheaperThan
for the classes that
represent the inventory in a grocery store.
Design the appropriate abstract class that eliminates as much repetition in the code as possible. Solution
Problem 19.1. Solution
If you feel that you really understand the concepts covered in these etudes, you may do only two out of four. However, make sure you know how to solve all of them.
One bad apple can spoil a bunch. In this problem we practice designing
methods for lists of data. The classes LoA
, MtLoA
,
and ConsLoA
represent a list of apples, for each apple we
record its weight, and whether it is good or spoiled. Design these
classes.
Now design the following methods that deal with lists of apples:
Method countGood
that counts all the good apples in the
list of apples.
Method onlyGood
that produces a list of all good apples
in the list of apples.
Method biggest
that finds the weight of the heaviest
good apple in the list of apples.
Method sort
that produces a list of apples sorted by
their weight.
Now rewrite the first three methods by using the accumulator style:
Design the method countGood2
that invokes
countGoodAcc
where countGoodAcc
uses an
accumulator to remember known values.
Design the method onlyGood2
that invokes
onlyGoodAcc
where onlyGoodAcc
uses an
accumulator to remember known values.
Design the method biggest2
that invokes
biggestAcc
where biggestAcc
uses an
accumulator to remember known values.
The goal of this exercise is to design an interactive game. The rat lives in a cage and rambles around looking for something to eat. There are globs of stuff around - some are food and some are poison. If the rat does not eat for some time, he starves to death. If the rat finds a food glob, his life expectancy increases as he eats the food. If he finds poison, he dies instantly.
Rat’s life is simulated on a canvas. The rat moves in response to the key events - as the user hits the arrow keys, the rat moves in the corresponding direction. The world starts with a collection of globs of food and poison. As the timer ticks away, the rat gets hungrier and closer to death, unless he finds some food to eat.
Design the class(es) that represent the rat, and design the methods that simulate the rat’s behavior:
Analyze the information needed to represent the rat. Make examples (in English) of several instances of a rat. Then, design the class(es) that represent the rat in this game. And we do not have to remind you that ...
Design the method that consumes a String
that represents a key
event and produces a rat that has moved in the direction specified
by the key event. Of course, that the rat only moves if one of the
appropriate keys was hit.
Design the method canEat
that determines whether the
rat is close enough to the given location (where, presumably, some
food or poison lies).
Design the method eatFood
that produces a rat that
consumed the given amount of food.
Design the method poison
that produces a dead rat.
Design the method starve
that produces a new rat, one
hour hungrier that this rat, possibly a dead rat.
Design the method draw
that displays the rat on the given
Canvas
.
Design the class RatWorld
that contains one rat. Design the
methods draw
, erase
, onKeyEvent
that
allow you to show the rat and control the moves using the arrow
keys. You will need to implement the skeleton of the method
onTick
as well — you learned how to do it in the lab.
Now design the onTick
method that just invokes the
starve
method in the class(es) that represent a
rat. Additionally, if the rat is dead, the game ends by returning
the result of the endOfWorld("The rat died.")
method
invocation.
Design the classes that represents the globs of food or poison in the rat cage. Then add methods that simulate the rat’s interaction with the globs (finding a glob, eating it, etc.). Note, that the rat’s life expectancy increases proportionately to the amount of food in each food glob. However, any amount of poison is fatal to the rat.
Analyze the information needed to represent the globs. Make examples (in English) of several instances of a both kinds of globs. Then, design the classes that represent the globs in this game.
Design the method(s) foundGlob
that determine whether a
given rat has found
this glob. (Remember the method canEat
in the class(es) for
rats?)
Design the method feed
that produces a new instance of
the given rat
after he ate this glob. Remember the eatFood
method to the
class Rat
?
Design the method newGlob
that produces a new glob at
random - either poisonous of a food glob of a random weight (food
amount), at a random location within the given bounds.
Design the method draw
that will display a glob in the
given Canvas
. Make sure that the player can distinguish
visually between the food globs and poisonous globs. It would also be
helpful, if the display indicated the relative sizes of the globs.
Add two globs to the class RatWorld
that contains one
rat. Modify the
methods draw
, erase
to show the glob as well.
Now modify the method
onTick
so that if the rat is close to the glob, he will eat
it.
Complete the game design, so that the rat moves in response to the key events (arrow keys), eats the food when close enough ot it, the eaten food is replaced, and the game ends if the rat ate poison or starved to death.
Design the class(es) that represent a list of globs and complete the game design.
Design the classes that represent a list of globs in this game.
Design the method draw
that displays this list of globs
in the given Canvas
Design the method feedRat
that produces a new rat that
consumed the first glob in this list that was close enough to the rat.
Design the method removeEaten
that replaces the first
glob in this list that was close enough to the rat, (so that the rat
ate it), with a new randomly chosen glob randomly placed it in the field.
Modify the RatWorld
so that instead of one glob, it
contains a list of them. Modify the onTick
method so that
the rat now has a choice of globs among all globs in a list of
globs.