home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
explorers
Due Nov.08 (Fri) 23:59.
(Bring hardcopy to the following class.)
This homework adds one last function to what was originally listed.
Due-date moved to Friday night, and we will talk about it in lab as well.
There will also be another assignment which builds on this,
to make in interactive game.
Modeling explorers
Start a new project, and copy in your
class Treasure
from previous homework.
We will add a second class to the same project.
An Explorer has a name,
a current room,
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 (and it disappeared),
and instead their right pocket now contains lint.
More precisely: add third Treasure object to
your picture (the new lint which we want to exist),
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.
-
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.
-
Write a function static readTreasure(Scanner,boolean),
which has the scanner read a name, description, weight, and image-URL,
and returns a new Treasure with those fields.
If the provided boolean is true,
then this method also prompts the user (typing at the keyboard) for the info (print one line
before reading each piece of information from the Scanner).
If the provided boolean is false, it does not print prompts to the user
(but still reads from the keyboard).
Tips:
-
Use java.util.Scanner#nextLine to read each piece of info.
For the weight, we're reading a String but we actually want a double;
we can convert it by passing that string to java.lang.Double#parseDouble
Unintuitive subtlety (optional):
You might think we should call java.util.Scanner#nextDouble instead.
You certainly can do that, but if so then you'll want to immediately follow it with
a(nother) call to java.util.Scanner#nextLine, for a non-obvious
reason:
you need to consume the newline-character that sits at the end of the number, and before
the next line.
Calling that funciton will return an empty-string (which you can then ignore), but it
does mean your next call to nextLine will be ready to read the
next line rather than the remainder of the current line (which is length 0).
This extra work is needed, in general, only when you are mixing calls to
nextLine with calls to nextDouble etc..
If you only use nextDouble() and next(),
you wouldn't need to worry about this.
-
You can call this function
Scanner scan = new Scanner(System.in);
followed by
readTreasure(scan,true).
-
You can also call it with a Scanner that takes its characters from a
string, rather than the keyboard!
readTreasure( new Scanner("a small nugget\nIt glints of gold.\n0.05\nhttps://collections.nmnh.si.edu/media/?irn=10246678"), false )
This is particularly helpful, because it lets us write some unit-tests!
(Be sure to write at least two, now that you know this.
You might even make one scanner that reads from a string-with-eight-lines,
and make two calls to readTreasure passing it that same scanner.
-
We'll even use it in a future assignment, to read from a file.
modeling Rooms
-
Make a class to represent a Room:
A room has 1 treasure and a description.
Later, we'll add more fields: other rooms to the north, south, east, and west!
-
Modify Explorer so that it has a current-Room.
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.
home—info—lects—labs—exams—hws
D2L—tutor/PIs—zoom (snow day)
Object120 + its docs—java.lang docs—java.util docs
 This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland radford.edu |
 |