List the 8 primitive predicates from the Robot class that can be used in an “if” clause:
anyBeepersInBeeperBag() facingNorth()
nextToARobot() facingSouth()
nextToABeeper() facingEast()
frontIsClear() facingWest()
Rewrite the following section of code to simplify it:
if (frontIsClear())
{
move();
}
else
{
turnLeft();
move();
}
if ( ! frontIsClear())
{
turnLeft();
}
move();
Rewrite the following section of code to simplify it:
if (facingWest())
return false;
else
return true;
return !facingWest();
When is each of the following iteration structures used?
while - Indefinite loop to perform instructions an indefinite number of times (until a certain test fails)
(True / False) The choice of which loop construct to use (which tool) is an arbitrary decision left up to you and your preference. It depends on the requirement.
List the 4 steps for building a while loop. Also, in section 6.6, what are the 2 things we should ALWAYS check when writing any loop to help verify its correctness.
Step 1) Identify what is true when the loop is finished (this is always easier than determining what is false while it is still executing)
Step 2) Use the opposite form of step 1’s result as the for the loop. You just need to negate the entire thing. You may need to use Morgan’s Law to do this.
Step 3) Make progress toward the goal (completion of the loop) within the loop
Check 1) Show that the method works correctly when the initial situation results in the test being false.
Check 2) We must show that each time the loop body is executed, the robot’s new situation is a simpler version of the old situation. By simpler, we mean that the robot now has less work to do before finishing the loop.
What are the two possible outcomes when a robot of the Robot class executes the following method (assuming you do not know what the world looks like)? Assume that faceEast is defined correctly.
public void goEast()
{
faceEast();
while (facingEast())
{
move();
}
}
Answer: The robot either runs into a wall or it is in an infinite loop
Write the correct syntax for the selection and iteration control structures below. You should use generic statements such and
a) if( )
{
}
else
{
}
b) for( int ; ; )
{
}
c) while( )
{
}
Rewrite the following section of code to simplify it:
if(frontIsClear())
{
if (nextToARobot())
{
move();
}
else
{
move();
turnRight();
}
}
else
{
if (nextToARobot())
{
move();
}
else
{
putBeeper();
turnRight();
}
}
No simplification.
Write a method to perform the following logic:
Move forward to a street corner, pick up all beepers on the street corner, then turn off.
public void pickUpBeepers()
{
move();
while ( nextToABeeper() )
{
pickBeeper();
}
turnOff();
}
Write a section of code in the SmartBot robot class to perform the following logic:
If the robot is facing east or north, then this robot should turn west and move forward until it runs into a wall or a boundary.
if ( facingEast() || facingNorth() ) {
faceWest();
while ( frontIsClear() )
{
move();
}
}
Write a for loop to pick up five beepers in a straight line.
for ( int i=1; i<=5; i++ )
{
pickBeeper();
move();
}
Name the three Boolean operators: ! (not), && (and), || (or)
Nested instructions: IF instruction nested within the THEN or ELSE clause of another IF instruction.
Predicate: Boolean method (i.e. frontIsClear() )
Execution equivalence: Two fragments of code have execution equivalence if they perform exactly the same in all situations.
Bottom factoring: Factor out the common instructions from the bottom of the THEN and ELSE clause and place these instructions immediately below the IF instruction.
Top factoring: Factor out the common instructions from the top of the THEN and ELSE clause and place these instructions immediately before the IF instruction. Note: Top factoring does not always work.
Short-circuit evaluation: Examples of short-circuit evaluation are as follows:
If you have test that has an && operator and the first boolean expression is false, then you do not have to check the second boolean expression (since the test is automatically false).
If you have test that has a || operator and the first boolean expression is true, then you do not have to check the second boolean expression (since the test is automatically true).
THEN clause: Instructions that get executed when the IF instruction’s test is TRUE. (within brackets immediately after the if reserved word).
ELSE clause: Instructions that get executed when the IF instruction’s test is FALSE. (within brackets immediately after the else reserved word).
for loop: Loop used to execute a set of instructions a definite number of times.
while loop: Loop used to execute a set of instructions an indefinite number of times (while a specified condition is true).
int variable: Integer variable, such as “i” used in a for loop.
i++ and i-- : i++ means to increment i by 1 (i=i+1;). i-- means to decrement i by 1 (i=i-1;)
Infinite execution: When the instructions within a loop execute without the loop ending.
Fence post problem: If we order five fence sections, we need six fence posts. This is a situation where we must do something either before or after the loop to solve the given problem, since the instructions in the loop do not entirely solve the problem for either the first time through the loop or the last time through the loop.
Beyond-the-horizon situation: Situations that are legal, but could cause problems. An example is when we try to create a box that is next to one of the boundaries.
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