AP Computer Science A – Karel J Robot

advertisement
AP Computer Science A – Karel J Robot
Review Sheet Chapters 4 SOLUTIONS
Name: ________________________
Date: _____________ Period: _____
1. Define the following terms using YOUR own words/diagrams (to prove that you
understand it). Don’t use a “book definition”.
• reference: name that represents something (i.e. bot1 could be a reference for a
type of robot). Variable and reference mean the same thing.
• object: an instance of a class
• class: a definition of an object. It contains the object’s code.
• abstraction: Mechanism and practice to reduce and factor out details so that one
can focus on a few concepts at a time (i.e. abstracting common code and putting
it into a method)
• abstract class: Special class to provide common code used among sub-classes, yet
allow sub-classes to have some specific differences
• abstract method: Method in an abstract class that must be redefined in another
class that extends the abstract class
• polymorphism: Objects in different classes can behave differently when sent the
same message
• initialize: Set the value of a variable to a specific value for the first time.
For example: karel = new UrRobot(1, 2, North, 0);
• assignment: Set the value of a variable to a specific value Use the equal sign (=)
to denote assignment. For example: karel = new UrRobot(1, 2, North, 0);
• instance variable: The things that an object remembers. These are the variables
decared in the object class outside of the methods (not inside any method’s
brackets)
• null: Special value to indicate that the reference to the variable does not refer to
anything at all (i.e. UrRobot karel = null).
•
parameter: Information sent to a method. When we instantiate a new robot
object, we are passing 4 different parameters which are used by the constructor
method. Parameters can be of any type.
2. What’s the difference between class and object?
A class is basically a definition, and contains the object's code. An object is an
instance of a class. For example there is one BetterRobot class, but you could
instantiate (construct) many BetterRobot objects.
Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class
3. Give an example of a NullPointerException:
When a message is sent to an object that has not been instantiated.
UrRobot karel;
karel.move();  This causes a NullPointerException
4. What would a move() method look like for a class that extends superclass UrRobot?
Create a robot class for a type of robot where the meaning of move is: drop a beeper at
the current location and then go forward 2 steps.
public void move()
{
putBeeper();
super.move();
super.move();
}
5. Using the following inheritance diagram, write code that would demonstrate
polymorphism. You must give 2 different examples using 2 different types of references.
You should document your code stating which statements are polymorphic and why.
SportRobot mike;
mike = new SoccerRobot(1,1,North,0);
mike.move(); // The move() method is redefined in the SoccerRobot class
mike.play();
Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class
mike = new TennisRobot(5,5,North,0);
mike.play(); // play() method is defined differently in SoccerRobot and TennisRobot
6. Using the diagram above, which of these are legal (i.e., will compile)? Highlighted in
red is legal
a. SportRobot tina = new SoccerRobot(…..);
tina.play();
b. SportRobot tina = new SoccerRobot(…..);
tina.move();
c. SportRobot tina = new SoccerRobot(…..); // kickGoal() is not valid for SportRobot
tina.kickGoal();
d. SportRobot tina = new SportRobot(…..); // Can’t construct SportRobot (abstract class)
tina.play();
e. UrRobot tina = new TennisRobot(…..); // play() is not valid for UrRobot class
tina.play();
f. UrRobot tina = new SoccerRobot(…..);
tina.move();
Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class
Download