130 likes | 147 Views
Extending Robot s. Tired of writing turnRight every time you start a new karel project? How do we avoid re-writing code all the time? Inheritance: a fancy term stolen by computer scientist to indicate the capability to pass on methods to other (sub)classes. How does it work?
E N D
Extending Robots • Tired of writing turnRight every time you start a new karel project? • How do we avoid re-writing code all the time? • Inheritance: a fancy term stolen by computer scientist to indicate the capability to pass on methods to other (sub)classes. • How does it work? • When a new class is created, the new class is allow to directly inherit from ONE (super) class using the keyword extends karel_part2_Inheritance
Extending Robot public class SmarterRobot extends Robot { public void turnRight{…} public boolean twoBeepersOnCornerOrMore() {…} public void faceEast() {…} // plus other directions } • We will put all useful methods in one class and now instead of extending Robot, our new classes will extend(s) SmarterRobot and inherit all methods in SmarterRobot and Robot. • Robot is the superclass • SmarterRobot is the subclass. • A subclass extends the superclass karel_part2_Inheritance
What does SmartRobot look like? public class SmarterBot extends Robot { public void turnAround(){turnLeft(); turnLeft(); } public void turnRight(){turnLeft(); turnAround();} public void faceEast() {…} // plus other directions } karel_part2_Inheritance
How do the new Classes look? public class ABetterRobot extends SmarterRobot { // ABetterRobot can do everything a SmarterRobot can. // ABetterRobot can do everything a Robot can. // plus any new methods defined in the class public boolean newMethodsToFinishTask(){ ….} public boolean otherMethodsToFinishTask(){ ….} } karel_part2_Inheritance
you write faceEast()it belongs to the SmarterBot class karel_part2_Inheritance
What to do? • Ask the Robot if it is facing East. if (facingEast()) • Now what? If facingEast() returns false, the Robot needs change. • What can we do to make the Robot face East. • Change the Robots direction by turning karel_part2_Inheritance
What does the code look like? public void faceEast() { if (!facingEast()) { turnLeft(); } } Could the Robot turnRight? Then why did we use turnLeft? karel_part2_Inheritance
Does it work? • If we test this method, we find when the Robot is facing South or East, the faceEast() does turn the Robot to the East. • Why does it fail in the other situations? • How do we fix this method? karel_part2_Inheritance
What does the code look like? public void faceEast() { if (!facingEast()) { turnLeft(); } if (! facingEast()) { turnLeft(); } if (! facingEast()) { turnLeft(); } } karel_part2_Inheritance
Does it work? • This time when we test this method, we find the robot is always facing East after the faceEast() is invoke regardless of it previous direction. • Could we add another if statement? karel_part2_Inheritance
Is there an Easier way? • The previous implementation contained repeated code • The repeated code is executed an unknown number of times • When code is repeated an unknown number of timesThe proper control structure is: public void faceEast() { while(!facingEast()) { turnLeft(); } { • But we have not discussed while yet • The last PowerPoint presentation discusses loops karel_part2_Inheritance
being redundant again and again and againha ha if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) // is this { // test necessary? turnLeft(); } } karel_part2_Inheritance
Your Assignment • Finish implementing all the methods declared in SmarterRobot class. • Once you’ve implement all the methods, test you SmarterRobot by executing mainDriver1. • If your robot correctly travels to the entire maze, picks six beepers and leaves seven beepers at corner street 2 and avenue 12 and stops at street 3, avenue 12 facing North, you have successfully completed this projects. • See handout (karel_part2_inheritance.doc) for details. karel_part2_Inheritance