Ap computer Science a – Karel j robot



Yüklə 22,96 Kb.
tarix24.12.2017
ölçüsü22,96 Kb.
#17018

AP Computer Science A – Karel J Robot Name: ________________________

Review Sheet SOLUTIONS - Chapter 5 Date: _____________ Period: _____


  1. List the 8 primitive predicates we learned about that can be used in an “if” clause:
    frontIsClear() facingNorth()
    nextToABeeper() facingSouth()
    nextToARobot() facingWest()
    anyBeepersInBeeperBag() facingEast()



  2. 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.



  3. Write the correct syntax for an if/else statement below. You should use generic statements such as and

if ()

{



}

else

{



}



  1. 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.




  1. 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.




  1. 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();




  1. 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();
    }




  2. 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();



  3. 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




  1. Rewrite the following section of code to simplify it:



if (frontIsClear())

move();


else

move();



move();




  1. Describe the following terms:

    1. Name the three Boolean operators: && (AND), || (OR), ! (NOT)

    2. Nested instructions: if statement within an if statement

    3. Predicate: Boolean method (returns true or false)

    4. Execution equivalence: When a fragment of code executes the same in all situations

    5. 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.

    6. 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.

    7. 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)

    8. THEN clause: These are the instructions that are executed if the boolean expression is TRUE.

    9. ELSE clause: These are the instructions that are executed if the boolean expression is FALSE.



  1. 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

Yüklə 22,96 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə