home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
hw06
hw06
Due Mar.26 (Mon) 17:00.
Short answer
-
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
-
(A one-character answer:) The general syntax of a for-each loop is:
for ( Type namevariable expressioncollection ) expressionloop-body
modeling Treasures
Start a new project, and copy in your
class Treasure from hw04.
We will add a second class to the same project.
An Explorer has a name,
and two pockets (left and right)
which each contain one Treasure.
-
Draw a picture of three objects:
two Treasures (neither of which happens to
have the name “lint”), and one Explorer
who has 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”.
-
Copy/paste your picture from above, and modify it
to represent what things would look like if an Explorer
dropped the Treasure in their right pocket, which
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 toString method for Explorers.
- Write a method String inventory(), which
returns a detailed listing of the Explorer's
two pockets.
- 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(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 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.
1
-
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.”
Turn in documentation, code, and unit-tests for Explorer.
(Note that Treasure wasn't actually modified for this
homework, so you don't need to turn in any printouts of that class.)
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.
As we've mentioned,
remember to declare every field and method either
public or private.
1If 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