AP Computer Science A – Karel J Robot Name: ______SOLUTIONS - Review Sheet Chapters 1-3 Date: ______Period: _____

1. In the robot world – a) streets run which directions? East-west b) avenues run which directions? North-south c) the range of street numbers are: 1-infinite d) the range of avenue numbers are: 1-infinite e) the three things which can be placed in the world are: robot, wall, beeper f) which one of these things is truly an object that can be acted on? robot

2. What effect do the following instructions have? a) move - move one corner in the direction it is facing b) turnLeft – robot turns 90 degrees left c) pickBeeper – robot picks up a beeper d) putBeeper – robot puts down a beeper e) turnOff - robot turns off and no other instructions can be executed

3. What use do the following have in the Java programming language? a) { } – groups code; used to begin and end a section of code (i.e. a method or a class) b) extends – extends a superclass; the public superclass methods are available to this class c) void – method does not return a value. The method does something instead of remembering something. d) public (used in front of method headings) - method can be accessed from other classes e) super (as used in the constructor) – stands for superclass. When used in the constructor, it executes the superclass constructor. When it is used as the object, it means to run the method from the superclass. f) // - single line comments g) /* */ - multiple line comments

4. Explain “error shutoff” – its causes and effects. Give an example of each. Error shutoff is when the robot cannot perform an instruction.

Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class Examples: 1) a move is executed but a wall or boundary is in the way. 2) Robot has not beepers but tries to put down a beeper. 3) Robot tries to pick up a beeper on a beeperless corner.

5. Explain the following errors and their effect on execution. Give examples: a) lexical - Word is not in its dictionary (compiler catches) b) syntax - Incorrect grammar, punctuation (compiler catches) c) intent - Program terminates successfully, but there is a logic problem d) execution - Can’t perform what you ask at run-time (i.e. moving into wall)

6. Explain “stepwise refinement”. Technique for writing modules which are concise, correct, easy to read/modify/understand. Write main task first, breaking up the BIG task into smaller tasks (using methods) – then take each method one at a time and also break it up --- continue until each method is compact and singular in focus.

7. Define the following Java related words: a) JDK - Java development kit – programming tools for developing Java (created by Sun) b) OOP - Object-oriented programming – programming paradigm that uses objects and their interactions to design programs and applications c) OOD - Object-oriented design – design using objects instead of procedures d) JVM - Java virtual machine – JVM’s read the compiled file (.class file) and run it. There is a separate and distinct JVM for each operating system (XP, MAC OS, Windows, etc). e) SDK - Software development kit – development tools to allow programmer to develop programs f) IDE - Integrated development environment – software application that provides a comprehensive environment for software development. It typically allows you to create/modify code, compile it, run it, and debug it. g) jar - Java ARchive - used for aggregating many files into one. It is generally used to distribute Java classes and associated metadata h) JRE - Java Runtime Environment – See JVM

Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class 8. What do the following terms mean: a) Inheritance – This is when you extend another class (superclass) and then the subclass can use any of the public methods in the superclass. b) Encapsulation - the bundling of data with the methods that act on the data. The mechanism for encapsulation in Java is the class definition. c) Abstraction - Putting the code into a method, so that we no longer need to write that code again. In addition, from the client’s point of view, it is no longer concerned with how the method does its job. d) Class - A class is a description of an object and contains the methods that act on the object. e) Method – a set of instructions that performs an action on an object f) Superclass – When extending a class, this is the class that is being extended (i.e. if BetterRobot extends UrRobot, then UrRobot is the superclass). g) Subclass - When extending a class, this is the class that is extending another class (i.e. if BetterRobot extends UrRobot, then BetterRobot is the subclass). h) Constructor – The constructor method is the method used to create the object. It gets called when the new keyword is used (i.e. when the following statement is executed, the constructor method in BetterRobot is run: BetterRobot bob = new BetterRobot(1, 1, North, 3); i) Java reserved words – These are special terms used by Java, such as class, void, new, public). You cannot used these words as variable names, since it will confuse Java. Names of classes (such as UrRobot, DiamondPlanter) are not reserved words. j) is-A relationship – This is used to signify that object1 extends object2 (i.e. BetterRobot is-A UrRobot). k) Any other terms introduced in chapters 1-3

Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class Programming: 9. Completely define a new class of robot called ReviewBot which can perform all of the following instructions. While writing this class, you should take advantage of any classes (i.e. BetterRobot) that we’ve written: a. move b. turnLeft c. jumpAndSpin (move one block forward and then face the opposite direction) d. turnOff e. moveFourAndDrop (move forward 4 blocks and then drop a beeper) f. putBeeper g. pickBeeper h. turnAround (180°)

public class ReviewBot extends BetterRobot { public ReviewBot (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(); } }

See the Back ->

Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class 10. 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 ReviewBot class above (assume it works regardless of what you wrote above).

public class ReviewBotTester implements Directions { public static void main(String[] args) { // your client code goes here...

public static void main(String[] args) { ReviewBot robot = new ReviewBot(1, 1, South, 2); robot.turnAround(); robot.moveFourAndDrop(); robot.turnAround(); robot.move(); robot.move(); robot.move(); robot.putBeeper(); robot.jumpAndSpin(); robot.turnOff();

} static { // don’t worry about this code } }

Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class