AP Computer Science A – Karel J Robot Name: ________________________
Review Sheet Chapters 4 SOLUTIONS Date: _____________ Period: _____
-
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.
-
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.
-
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
-
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();
}
-
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();
mike = new TennisRobot(5,5,North,0);
mike.play(); // play() method is defined differently in SoccerRobot and TennisRobot
-
Using the diagram above, which of these are legal (i.e., will compile)? Highlighted in red is legal
-
SportRobot tina = new SoccerRobot(…..);
tina.play();
-
SportRobot tina = new SoccerRobot(…..);
tina.move();
-
SportRobot tina = new SoccerRobot(…..); // kickGoal() is not valid for SportRobot
tina.kickGoal();
-
SportRobot tina = new SportRobot(…..); // Can’t construct SportRobot (abstract class)
tina.play();
-
UrRobot tina = new TennisRobot(…..); // play() is not valid for UrRobot class
tina.play();
-
UrRobot tina = new SoccerRobot(…..);
tina.move();
Original document provided by www.apComputerScience.com and modified by Mr. Smith for his specific class
Dostları ilə paylaş: |