AP Computer Science A – Karel J Robot Name: ________________________
Review Sheet SOLUTIONS - Chapter 5 Date: _____________ Period: _____
-
List the 8 primitive predicates we learned about that can be used in an “if” clause:
frontIsClear() facingNorth()
nextToABeeper() facingSouth()
nextToARobot() facingWest()
anyBeepersInBeeperBag() facingEast()
-
In order for a new class (subclass) that you write to be able to use the 8 primitives above, the class must have been derived from the Robot class at some point in the inheritance chain.
-
Write the correct syntax for an if/else statement below. You should use generic statements such as and
if ()
{
}
else
{
}
-
Rewrite the following section of code to simplify it:
-
if (frontIsClear())
{
move();
}
else
{
turnLeft();
move();
}
|
if (!frontIsClear())
{
turnLeft();
}
move();
We used bottom factoring to do this.
|
-
Rewrite the following section of code to simplify it:
-
if (nextToABeeper())
{
move();
}
else
{
move();
turnRight();
}
|
Cannot be simplified except for taking out brackets on the then clause. Top factoring does not work in this case since moving to a new location will affect the nextToABeepr() test.
|
-
Rewrite the following section of code to simplify it:
-
if ( ! nextToARobot() )
return false;
else
return true;
|
if ( nextToARobot() )
return true;
else
return false;
OR
return nextToARobot();
|
-
Write a section of code in the SmartBot robot class to perform the following logic:
If there is a beeper and another robot on the same corner as this robot, then this robot should pick up the beeper and move forward one block; else, this robot should stay where he is.
if ( nextToABeeper() && nextToARobot() )
{
pickBeeper();
move();
}
-
Write a section of code in the SmartBot robot class to perform the following logic:
If there is a wall in front of this robot or this robot is facing west, then this robot should turn left and move forward one block; else, this robot should move forward one block.
if ( ! frontIsClear() || facingWest() )
{
turnLeft()
}
move();
-
Rewrite the following section of code to simplify it:
-
if (nextToARobot())
{
putBeeper();
turnRight();
move();
turnLeft();
move();
}
else
{
putBeeper();
turnLeft();
move();
turnLeft();
move();
}
|
putBeeper();
if (nextToARobot())
{
turnRight();
}
else
{
turnLeft();
}
move();
turnLeft();
move();
In this example you can use both top factoring and bottom factoring
|
-
Rewrite the following section of code to simplify it:
-
if (frontIsClear())
move();
else
move();
|
move();
|
-
Describe the following terms:
-
Name the three Boolean operators: && (AND), || (OR), ! (NOT)
-
Nested instructions: if statement within an if statement
-
Predicate: Boolean method (returns true or false)
-
Execution equivalence: When a fragment of code executes the same in all situations
-
Bottom factoring: Extracting out the common instructions at the bottom of both the THEN and ELSE clause and placing them after the if statement. Always works.
-
Top factoring: Extracting out the common instructions at the top of both the THEN and ELSE clause and placing them before the if statement. Doesn’t always work.
-
Short-circuit evaluation: If a boolean expression uses an && (AND) between too expressions, it does not have to evaluate the second one if the first is false (the entire expression will be false). If it uses the || (OR) operator, then it does not have to evaluate the second expression if the first is true (the entire expression will be true)
-
THEN clause: These are the instructions that are executed if the boolean expression is TRUE.
-
ELSE clause: These are the instructions that are executed if the boolean expression is FALSE.
-
Complete the following class, adding any additional methods you might need to help you (note: you should at this point be able to recognize which methods need further modularization and which can be written only in terms of primitives - please modularize where appropriate).
public class JumpHarvestAndTest extends Robot {
//Assume constructor has already been written
//precondition: robot is facing east and standing next to base of a hurdle (wall) that is 1-2 blocks high
//postcondition: robot is facing east and is standing on other side of the hurdle (wall) at the base
public void jump()
{
turnLeft();
move();
if ( ! rightIsClear() )
{
move();
}
turnRight();
move();
turnRight();
move();
if (frontIsClear())
{
move();
}
turnLeft();
}
Friendly reminder: In order to help ensure the reading of the chapter, I usually put a question or two from the reading (which I didn’t cover in class) on the test. :-)
Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class
Dostları ilə paylaş: |