home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
lab13a
Objects containing Objects
We will resume our
Hideout example from yesterday's lecture.
Source:
lect12a-sect04-hideout.jar
see
how-to-get-jar-files-into-BlueJ—Downloading .jar files into BlueJ.
We won't turn in today's work,
though it will practice the concepts that will be needed for the last hw.
-
Recall that a Hideout contains
-
a String address,
-
a Robot pres,
-
a Robot vp,
-
and
a Robot[] crew
(called “members” in the 11am section)
-
Also recall:
Lesson:
A method should process one type of data --
process nested data with helper functions.
(As things get more and more complicated, it gets too
hard to keep track of things.)
Hideout methods should only work
with Hideout fields/info --
if there is code about Robots,
make it a method inside class Robot!
-
Later today, you'll write a function which swaps the president
with a crew-member.
On the board, together:
What does the 'before' picture look like?
What will the 'after' picture look like?
What code is required to get from 'before' to 'after'?
-
Why doesn't the following two lines of code work?:
int a = 93
int b = 17;
// Now, we try to swap the contents of these two local variables:
a = b;
b = a;
// What do things look like?
|
How can we fix this?
-
Write the function Hideout#toString().
(Note that Robot#toString()
and
Song#toString() are already written.)
-
Complete Hideout#deepEquals(Hideout),
which just compares two Hideouts,
comparing field-by-field for equality1
(Note that Robot#deepEquals(Robot) has already been written,
and that
Song#equals(Song) already checks for "deep" equality.)
-
A method to modify the array:
/** Swap this Hideout's president and a crewmember.
* @param crewIndex The index of which crewmember to make president.
* It must be an integer in [0,this.crew.length).
*/
void elect( /* Hideout this, */ int crewIndex ) { /* stub */ }
|
A 'Before' picture is drawn for you on the board.
Draw the picture after calling elect(1).
-
Complete the function coolestRobot (barely started in lecture),
which returns the coolest Robot in this entire Hideout.
(You should complete the lab at least this far.)
-
/** Swap this Hideout's president and the most popular crewmember.
*/
void holdElection() { ... }
|
We'd like to be able to re-use coolestRobot without change,
but we can't quite do that -- we need to know not just the
coolest crewbot, but its index.
(This will at least allow us to directly re-use elect.)
However, we can retroactively re-factor our code to minimize work.
-
Write boolean isInRevolt(), which determines
whether any crew-*members* of this Hideout would attack any other crew-member.
Hint:
A handy helper method would take any Robot r
and any Robot[] crowd,
and determine whether r would pick a fight with
any member of crowd.
Once this is written, how can you use it to determine whether
each crewmember might combat any other member of the crew?
Hideout.java
(and one helper function (willBattleAnyOf) added to
Robot.java)
1
In the past we've just called this “equals”,
but here we decide that we want to allow for two
Hideouts that look the same (are deepEquals)
to actually be considered different (not equals) —
like identical twins, who are not equals
despite being deepEquals.
↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs