Ap computer Science a – Karel j robot Name: Review Sheet Chapters 5-6



Download 24.05 Kb.
Date28.01.2017
Size24.05 Kb.
#9526
AP Computer Science A – Karel J Robot Name: ________________________

Review Sheet Chapters 5-6 (IF and Loops) Date: _____________ Period: _____

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


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

    if (frontIsClear())

    {

    move();



    }

    else


    {

    turnLeft();

    move();

    }


    if ( ! frontIsClear())

    {

    turnLeft();

    }

    move();

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

    if (facingWest())

    return false;

    else

    return true;



    return !facingWest();

  4. When is each of the following iteration structures used?

    1. while - Indefinite loop to perform instructions an indefinite number of times (until a certain test fails)

    2. for - Definite loops to perform instructions an explicit number of times

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

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

Step 4) Do whatever is required before and/or after the loop to ensure that we solve the given problem

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.


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

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

}


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

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


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


  4. Write a for loop to pick up five beepers in a straight line.

    for ( int i=1; i<=5; i++ )
    {
    pickBeeper();
    move();
    }


  5. Describe the following terms:

    1. Name the three Boolean operators: ! (not), && (and), || (or)

    2. Nested instructions: IF instruction nested within the THEN or ELSE clause of another IF instruction.

    3. Predicate: Boolean method (i.e. frontIsClear() )

    4. Execution equivalence: Two fragments of code have execution equivalence if they perform exactly the same in all situations.

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

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

    7. Short-circuit evaluation: Examples of short-circuit evaluation are as follows:

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

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

    8. THEN clause: Instructions that get executed when the IF instruction’s test is TRUE. (within brackets immediately after the if reserved word).

    9. ELSE clause: Instructions that get executed when the IF instruction’s test is FALSE. (within brackets immediately after the else reserved word).

    10. for loop: Loop used to execute a set of instructions a definite number of times.

    11. while loop: Loop used to execute a set of instructions an indefinite number of times (while a specified condition is true).

    12. int variable: Integer variable, such as “i” used in a for loop.

    13. i++ and i-- : i++ means to increment i by 1 (i=i+1;). i-- means to decrement i by 1 (i=i-1;)

    14. Infinite execution: When the instructions within a loop execute without the loop ending.

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

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


Download 24.05 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page