AP CS a Karel J Robot

AP CS a Karel J Robot

AP CS A – Karel J Robot

Review Sheet Chapters 1-3 - ANSWERS

1. In the robot world –

a)streets run which directions? east/west

b)Avenues run which directions? north/south

c)streets are numbered: 1 to infinity

d)avenues are numbered: 1 to infinity

e)the three objects which can be placed are: robots/walls/beepers

2. What instruction gives a robot the ability to

a)change his location – move()

b)rotate 90 counter-clockwise – turnLeft()

c)capture a beeper – pickBeeper()

d)deposit a beeper – putBeeper()

3. What effect do the following instructions have?

a)move – moves one corner in the direction the bot is facing

b)turnLeft – turns the robot 90 degrees counter-clockwise

c)pickBeeper – picks up a beeper

d)putBeeper – puts down a beeper

e)turnOff – turns the robot off

4. What use do the following have in the Java programming language

a){ } – marks the beginning and ending of a block

b)extends – enables inheritance

c)void – means that the function doesn’t return anything after it’s finished

d)public (used in front of method headings) – clients may call this method

e)super (as used in the constructor) – call the superclass’ constructor

5. Explain “error shutoff” – its causes and effects

run-time errors – execution halts

6. Explain the following errors and their effect on execution:

a)lexical – word not in dictionary

b)syntax – spelling error

c)intent – logic error

d)execution – run-time error

7. Explain “stepwise refinement”.

breaking down an algorithm into smaller and smaller pieces until each piece is cohesive(singular in focus) – aids in code re-use – enables one to take advantage of abstraction(not needing to know the details of something – you just get to use the method and don’t need to understand it’s internals)

8. Relate the following terms to each other using a few complete sentences: ‘inherits’, ‘superclass’, ‘subclass’, ‘extends’, ‘is-A’, ‘encapsulation’, and ‘abstraction’.

Note: I am not looking for something this extensive for your answers.

The word ‘class’ in java is a keyword. We can also use it as a concept. A class (as a concept) allows us to encapsulatemethods and data into a single entity. Encapsulation allows clients to work with the class in an abstract manner (i.e., client doesn’t need to know how the class works – it just gets to use its services, thereby being able to focus on what it wants to accomplish without being burdened with knowing how it is all being accomplished).

So, abstraction is a good thing – it allows us to solve bigger/better problems without worrying about the details. Abstraction also aids us in design – we can pseudocode an algorithm in terms of not-yet-written methods. This keeps our focus on the task at hand, enabling us to put off the details until a later time (at which point we’ll again take advantage of this design strategy when designing those details) – we continue with this design technique until we’ve achieved cohesion among all methods. Another type of abstraction is “procedural abstraction”, whereby one locates a commonly used segment of code within a single cohesive method (simple e.g., instead of having 3 turnLeft statements all over the place, we create one cohesive method containing those 3 statements and then call it turnRight).

Now, we go to make a new class of objects and want to take advantage of classes we’ve already made – so, we use inheritance. Our new class (the subclass) will inherit from an existing class (the superclass) using the keyword extends. When we choose a superclass for our new subclass, we should make sure that the following relationship makes sense: subclass is-A superclass (the so-called “is-A” test). For example, if I were going to make a BetterTurner robot class, I would extend UrRobot – because my BetterTurner is-A UrRobot (it can move, turnLeft, putBeeper, pickBeeper, and turnOff), but it can also (the new functionality) turnRight and turnAround. An example of improper use of inheritance – having, say, the SteepleChaser class extend the MileWalker class – ask yourself if: SteepleChaser is-A MileWalker (probably not a logical/good relationship).

Programming:

1. Completely define a new class called Review which can perform the following:

move

turnLeft

jumpAndSpin (move one block forward and then face the opposite direction)

turnOff

moveFourAndDrop (move forward 4 blocks and then drop a beeper)

putBeeper

pickBeeper

turnAround (180)

While writing this class, you should take advantage of any classes we’ve written in class.

// solution on next page

public class Review extends BetterTurnerRobot

{

// BetterTurnerRobot already has turnAround() and turnRight()

public Review (int st, int av, Direction dir, int beeps)

{

super(st,av,dir,beeps);

}

public void jumpAndSpin()

{

move();

turnAround();

}

public void moveFourAndDrop()

{

move();

move();

move();

move();

putBeeper();

}

}

2. Write a complete client program which will create a robot at the origin facing south with 2 beepers. The robot should then proceed to 5th street and 1st avenue to drop a beeper; then it should proceed to 2nd street and 1st avenue and deposit the last beeper. The robot should end up at the origin facing north. Try to use the least number of statements possible and you must use the Review class above (assume it works regardless of what you wrote above).

public class Sample implements Directions

{

public static void main(String[] args)

{

Review bot;

bot = new Review(1,1,South,2);

bot.turnAround();

bot.moveFourAndDrop();

bot.jumpAndSpin();

bot.moveFourAndDrop();

bot.jumpAndSpin();

bot.turnOff();

}

static {

// don’t worry about this code

}

}