ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws—java.lang docs—java.util docs
hw05:
gRUe, pt. I
Due Nov.05 (Mon) 17:00.
Solution (.jar)
The Grand Repository of Underground Entertainment (gRUe) is a
simple game of exploration, where an intrepid adventurer may journey
through various rooms, collecting and leaving behind various treasures.
gRUe will be written over the course of four weekly homeworks.
For part I, we will develop two classes:
Explorer, and
Treasure.
As usual, include javadoc code for each class.
Write these comments before writing the actual code;
the comments will help you focus on what the method does.
PIs have been instructed to not help you on a method,
if you don't have those comments already written.
Similarly, if you are requesting help on a method, and don't
have all other already-written methods called and tested from
your test method, you must do that before receiving later help.
-
A Treasure is a relatively simple class:
every treasure has three attributes: a name, a description, and
a weight (in lbs).
Some examples might be:
name: a small nugget
description: Upon close inspection, the nugget glints of gold!
weight: 0.05 lbs
name: a multi-colored pen
description: This fancy pen can write in blue, red, green, and plaid.
weight: 0.1lbs
name: chocolate egg
description: The size of an ostrich egg, but made entirely of dark chocolate!
weight: 35 lbs
- Create a class with those attributes.
- Write a constructor which takes the required information
as parameters.
- Write a toString method for Treasures.
- Write a static method testTreasure which creates
several Treasure instances, and prints
them to the console window with System.out.println.
(Make three examples, different from the one shown above.)
- Optional: Sometimes a Treasure's name
pretty much is all the description you need.
Overload the constructor so that if somebody creates a
new Treasure but only provides a name and a weight,
then the name is also used as the description.
(You may even read about
calling one constructor from inside another,
to avoid repeated code.)
- There are two treasures which will we will need
multiple instances of, in the world of gRUe: lint, and dirt
(which each weigh 0).
Write methods
createLint() and createDirt(),
which each return an appropriate newly-made Treasure.
(These methods should be static, since they are not behaviors
of specific Treasures; it makes sense to call this
method even if no other Treasures have ever
been constructed.)
- Revise TestTreasure to include a test of createLint.
- Write a Treasure method boolean isLint().
(Should this method be static? That is, does it make sense
to call this method even if no Treasure have
ever been created?)
How can you tell if a Treasure instance was one
created by createLint()?
We'll presume1
that just checking the instance's name will suffice.
When comparing Strings,
do you want to use == or .equals?
Does your code have any magic numbers &hellp; or magic strings?
- Revise TestTreasure to include a test of isLint.
-
An Explorer has a name,
and two pockets (left and right)
which each contain one Treasure.
- Create a constructor which takes just a name.
It creates a new Explorer with the given
name, and lint in each pocket.
- Write a toString method for Explorers.
- Write a static testExplorer method which creates
one explorer and prints it out to the console window.
As you write further methods, add further tests to this method.
- Write a method String inventory(), which
returns a detailed listing of the Explorer's
two pockets.
- Write
Treasure String dropLeft() replaces
the contents of the left pocket with lint
and returns a
String saying what happened,
e.g. “You drop a chocolate egg.”.
Similarly, write TreasureString dropRight(Treasure).
- Write method String grab(Treasure), which takes
a Treasure as a parameter.
-
If one pocket or the other contains lint, then this new
Treasure is placed into such a pocket,
and the method returns a message along the lines of
“You drop a lint. You pick up a chocolate egg.”.
-
If neither pocket contains lint, then the
pockets remain unchanged, and you return a message saying
something like
“Your pockets are full; you might want to drop something.”,
followed by the Explorer's inventory.
2
-
Implement a weight limit for Explorers:
if an Explorer tries to grab a Treasure
which would bring the
combined left- and right-pocket Treasures' weights
above the limit,
then the pocket stays the same,
and it instead returns a message along the lines of
“The chocolate egg is too heavy to pick up.”
1A slightly better way would be to double-check
both the name and the description, and the weight.
Can we have some infallible method, so that only
Treasures which were created through
createLint() will answer true
when asked isLint(), and all other Treasures
will answer false, no matter how its
constructor was called?
5 points extra credit for doing this.
back
2If you prefer, you can have grabbing a third item
summarily drop one of the the two existing items:
“You pick up the chocolate egg,
dropping a multi-colored pen”.
back
ITEC120-ibarland (incl. office hrs)—info—lectures—labs—hws—java.lang docs—java.util docs