Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.

Similar presentations


Presentation on theme: "1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com."— Presentation transcript:

1 1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com

2 2 Compare MileWalker to the basic UrRobot: 1)What is different? 2)What is the same? MileWalker can move 8 blocks MileWalker can move one block, turn left, pick up a beeper, put down a beeper, and turn off (just like UrRobot) In OOP terminology, it would make sense for MileWalker to inherit the capabilities of UrRobot.

3 3 STEP 1 Define a new class of robot (see next slide): When designing a new class (whether it’s a type of robot, car, bank account, etc.), the first question we are asking ourselves is “What can I steal?” !! In other words, one big benefit of OOD is the concept of code- reuse. There is no need to reinvent the wheel. (by the way, that doesn’t mean to copy your friend’s lab!! ) Also, what is different? We need to create new methods to reflect the differences.

4 4 Inserting MileWalker into the Inheritance Hierarchy UrRobot MileWalker move() moveMile() turnLeft() pickBeeper() putBeeper() turnOff() If you have an object (say, bob) of type MileWalker, what methods are available to bob? What if bob were of type UrRobot? the is-A relationship: a MileWalker is-A UrRobot

5 5 import kareltherobot.*; public class MileWalker extends UrRobot { public MileWalker ( int st, int av, Direction dir, int beeps ) { super(st, av, dir, beeps); } public void moveMile( ) { move(); move(); } } note: no object names preceding methods (why not?) - let’s look at client code to see why superclass (or parent class) subclass constructor invokes superclass’ constructor

6 6 STEP 2 Write application (client) to use your new object class (client is also known as a driver program). This should be a separate class from the object class. import kareltherobot.*; public class MileWalkerTester implements Directions { public static void main(String args[]) { MileWalker bob = new MileWalker(2, 1, East, 0); bob.moveMile(); // new instruction bob.move();// inherited instruction bob.turnOff();// inherited instruction } This client program is similar to your previous programs. Instead of constructing a UrRobot type of robot, we are constructing a MileWalker robot.

7 7 (don’t sweat this – it’s not computer science) These 4 method invocations may be necessary and may be placed first within the main() method of the client (driver) program: –World.reset(); –World.readWorld(“ch2_6_wld.txt"); –World.setDelay(50); –World.setVisible(true); –World.showSpeedControl(true); –Alternatively, you can place them in a static block (no need to understand what a “static block” is) within your driver class. A static block runs before the main() method runs.

8 8 You should end up with two classes: –an object class that defines the new type of robot –a client class which is used to test the new robot The client program should test several of the methods in the new robot class: –Make the robot move a single block, a mile (8 blocks), turn left, etc. Convention: class names begin with a Capital Letter, method names begin with a lowercase letter – if an identifier combines several words, use the “camelCase” technique. (It is important to follow convention.) We’ll now demo the whole process in jGRASP and look at MileWalker and MileWalkerTester.

9 9 Create a BetterRobot class –Create methods to: turnRight(), - turns right (does not move) turnAround(), - turns backwards (does not move) stepBackward(), - moves backwards and remains in same direction moveMile(), - 8 corners moveKiloMile() - 80 corners Any other methods that might be helpful?? Create a small client program named BetterRobotTester to test the BetterRobot class –Construct the new robot –Move the robot around the screen using inherited methods or the new methods created above


Download ppt "1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com."

Similar presentations


Ads by Google