home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
hw08
Explorers
Part (a): Short answer
Due Oct.12 (Fri) in class.
-
Frooble f1 = new Frooble();
Frooble f2;
f2 = new Frooble();
Frooble f3 = f1;
f3 = f2;
|
After evaluating all lines of the above the code fragment,
- How many Froobles exist?
- How many variables of type Frooble exist?
- f1 == f2?
- f1 == f3?
- f2 == f3?
- (Extra Credit, 1pt) f1.equals(f2)?
-
To test whether two String (reference)s s1,s2 have the same characters,
use s1 s2
-
To test whether two PizzaServer (reference)s p1, p2 refer to identically
the same object, write p1 p2
hw08-soln.html
modeling Treasures
Start a new project, and copy in your
class Treasure from hw06—constructors: Treasures.
We will add a second class to the same project.
Part (b), due Oct.15 (Mon) in class:
As usual:
Turn in the requested pictures,
your documentation,
and your test cases
(the code auto-generated by BlueJ when you recorded your tests).
You'll need to write stub versions of each method, but no actual code.
Don't turn in any of the code you wrote.
Note that this assignment doesn't have you modify any code
for class Treasure,
so you don't need to re-submit anything for that class.
When submitting to WebCT, it's fine to just attach your entire BlueJ folder.
Part (c), due Oct.17 (Wed) in class:
As usual,
turn in your source code for Explorer.
When submitting to WebCT, it's fine to just attach your entire BlueJ folder.
An Explorer has a name,
and two pockets (left and right)
which each contain one Treasure.
-
Draw a
“before”
picture involving three objects:
two Treasures (neither of which happens to
have the name “lint”), and one Explorer
who is “carrying”
those treasures in its pockets.
(There are no variables in this picture -- only objects.)
Note that the English concept
“an explorer carrying a treasure”
is represented in our program by
“an Explorer object has a reference to
a Treasure object”.
-
Draw a second “after” picture,
based on your previous one,
modifying it
to represent what things would look like if an Explorer
dropped the Treasure in their right pocket,
and instead their pocket now contains lint.
More precisely: add third Treasure object to
your picture (the new lint which suddenly exists),
and update the Explorer's fields appropriately.
Note that there are now three Treasures, although
the Explorer has no way to get (a reference to)
the “dropped” 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 method String inventory(), which
returns a detailed listing of the Explorer's
two pockets.
(What Treasure method will come in handy here?
Don't repeat code you already wrote in
class Treasure!)
Note: You don't need to write tests for this method1
(although you should certainly check to make sure it works).
-
Write a method public String toString() for Explorers,
Clarification:
which returns the Explorer's name followed by
their inventory.
Don't repeat any code you already wrote for inventory!
You don't need test cases for this function.
(The name “toString” is treated specially by Java;
you need to put the adjective “public”
in front of the signature, to get it to compile.2)
- Write String dropLeft() which 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 String dropRight().
(How will you make tests to verify that your method works
correctly?)
- 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 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.
3
-
For part (c),
go back and 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 (perhaps 50lbs.),
then the pocket stays the same,
and it instead the method returns a message along the lines of
“The chocolate egg is too heavy to pick up.”
As usual,
follow The Design Recipe.
PIs have been instructed to not help you on a method,
if you don't have its comments and test-cases already written.
1
Why no tests for this?
It would certainly be a good idea, but in practice,
when you are typing in the expected-result which is a long string
of conversational English,
it's a hassle to get the anticipated result character-for-character
correct.
↩
2
The error you get is something like “toString()
in Explorer cannot override toString() in
Object; attempting to assign weaker access privileges;
was public”.
We'll talk about what overriding is in week 13-14 or so.
↩
3If 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”.
↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs