1 / 13

Extending Robot s

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?

Download Presentation

Extending Robot s

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. 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

  4. 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

  5. you write faceEast()it belongs to the SmarterBot class karel_part2_Inheritance

  6. 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

  7. 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

  8. 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

  9. What does the code look like? public void faceEast() { if (!facingEast()) { turnLeft(); } if (! facingEast()) { turnLeft(); } if (! facingEast()) { turnLeft(); } } karel_part2_Inheritance

  10. 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

  11. 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

  12. being redundant again and again and againha ha if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) // is this { // test necessary? turnLeft(); } } karel_part2_Inheritance

  13. 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

More Related