For all the programming assignments, be sure to follow the design recipe.
Each problem asks you either to create a new class with some methods,
or to add a new method to a class you've already written.
For classes, you need to identify the names and types
of the instance variables, then write each of the methods.
For each method, go through
the usual contract, examples,
skeleton, inventory,
body, test steps.
I recommend writing the examples by using the
tester
library, as discussed in class on Feb. 11, so you
can easily run all your tests and confirm that they all pass.
Be sure to choose meaningful names for methods and parameters, and watch for opportunities to re-use methods you, I, or the textbook have already written.
Also turn in a log of time spent and errors encountered in the assignment, with brief comments describing each error ("mismatched parentheses" is self-explanatory, but more complex errors might need more description). You may do this using the PSP forms, or simply by keeping track in a text file or on paper and turning it in.
A ZooAnimal is defined (for this problem) as either a Lion, a Snake, or a Monkey. All animals have a name and a weight. Furthermore, different lions eat different amounts of meat per day; different snakes are different lengths; and different monkeys have different favorite foods.
Define the classes ZooAnimal
, Lion
,
Snake
, and Monkey
, with suitable instance
variables, constructors, and getters.
Add a method fitsInCage
which takes in a
ZooAnimal
and the weight capacity of a cage, and tells
whether this animal fits in that cage.
A shape is defined (for the purposes of this assignment) as
either a square, a rectangle, or a circle.
Define the classes Square
, Rectangle
,
Circle
, and Shape
, with the appropriate
instance variables, constructors, and getters.
All shapes have a perimeter and an area. Define the methods
perimeter
and area
, in such a way that one can
invoke both methods on any Shape variable.
If we want to work with shapes on a computer screen, each shape needs not only a size but a location: add a "center" property (a Posn) so that every Shape has a center.
Define a method contains
which takes in a Shape and
a Posn, and tells whether the shape contains the position.
A Manhattan tour guide wants to keep track of different
attractions in Manhattan. An Attraction
is either a
Museum
, a Restaurant
, or a
Shop
.
All three have a name and an Intersection
,
which consists of street number and an avenue number (for simplicity,
pretend there are no named avenues or streets).
In addition, a restaurant has a type of cuisine
(e.g. "Thai", "Mexican", "Uzbekistani", etc.) and a
typical price per person. A museum has an admission price and opening
and closing hours. A shop has opening and closing hours and a type
(e.g. "clothes", "musical instruments", etc.)
Define the relevant classes to represent this information, with the appropriate instance variables, constructors, and getters.
Define a method blocks
which takes two Intersections
and tells the number of blocks from one to the other. Note that you
can only walk along streets and avenues, not diagonally.
Define a method affordable
that operates on an attraction
and an amount of money and tells whether it's affordable on that
budget. (You can always get into a shop for free.)
You should end up with bunch of classes. You can put all of them in one BlueJ project, or use one project for each of the three problems. Zip them up and attach it to an e-mail to me. If you've entered your time and error logs using the on-line forms, I've already got them; if not, e-mail me your log files.
Note: I won't actually grade every one of these methods, but a representative sample; the rest are for practice. If I were to grade them all, the points would look something like this: