4. Test
Karel Chapter 4 Test
You may not use any resource materials for this test.
Partners
New laws have been passed and now robots can get married. A robot who
wants to get married must implement the Partner
interface. The
Partner interface consists of a public Partner getSpouse()
method as
well as a public void setSpouse(Partner x)
and a public boolean isAvailable()
method. When robots are delivered from the factory they
do not have a partner.
-
Write the
Partner
interface.- getSpouse: return the current spouse
- setSpouse: make the current spouse be the given robot
- isAvailable: false if the Partner has a spouse
-
Create a
LBot
that implements thePartner
interface. Example test code:
public static void main (String[] args) {
LBot sally = new LBot(4,2,East,10);
LBot harry = new LBot(5,2,East,4);
sally.setSpouse(harry);
if (sally.getSpouse() != harry) {
System.err.println("Sally just married Harry. What's going on?");
}
if (sally.isAvailable()) {
System.err.println("Sally should not still be available!");
}
}
Marriage
- The
JusticeOfThePeaceBot
can marry robots. The function ispublic boolean marry(Partner a, Partner b)
.- Write this function.
- Only marry if both robots are available.
- If either robot is unavailable, do not marry them!
- Return true if the partners get married, false if the marriage fails.
public static void testMarry()
{
LBot sally = new LBot(4,2,East,10);
LBot harry = new LBot(5,2,East,4);
JusticeOfThePeaceBot judge = new JusticeOfThePeaceBot(1,1,East,0);
boolean gotMarried = judge.marry(sally,harry);
if ( ! gotMarried
|| sally.getSpouse() != harry
|| harry.getSpouse() != sally )
{
System.err.println("they did not get married!?");
}
}
Relationships
Do one of the following:
-
(Know 4.8) The
FindRelationship
strategy is: move forward one step, if there is a robot there and it is available, change to NoStrategy, otherwise continue looking. -
(No 4.8) The
Courting
strategy is created byStrategy s = new Courting(Partner x)
. The strategy checks to see if the partner is available, and if so it puts a beeper down. Write theCourting
strategy.
public static void testCourt()
{
LBot sally = new LBot(4,2,East,10);
LBot harry = new LBot(5,2,East,4);
Courting c = new Courting(sally);
c.doIt(harry); // harry should place a beeper
sally.setSpouse(sally); // married herself to make harry go away
c.doIt(harry); // harry should do nothing
}