1 / 40

Enhancing Robot Programming with New Classes and Methods

Learn how to define new classes of robots and methods to extend a robot's vocabulary for easier programming and task execution. Step-by-step examples included.

lchu
Download Presentation

Enhancing Robot Programming with New Classes and Methods

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 the Robot Programming Language Chapter 3

  2. The robot programming language permits the robot programmer to specify new classes of robots. • These class descriptions provide specifications of new robot instructions. • Karel-Werke will then use the class descriptions to create robots able to interpret the new messages. New Classes of Robots

  3. Karel-Werke builds each robot with a dictionary of useful method names and their definitions. • Each definition must be built from simpler instructions that robots already understand. • By doing this, we can build a robot vocabulary that corresponds more closely to our own. • Given this, we can solve our programming problems using whatever instructions are natural to our way of thinking, and then we can provide robots with the definitions of these instructions. Dictionary-Like

  4. Task: Have the robot walk five miles (eight blocks = 1 mile). • Old way: karel.move(); karel.move(); karel.move(); karel.move(); karel.move(); karel.move(); karel.move(); karel.move(); karel.move(); karel.move(); // etc //(should have 40 instructions) Example

  5. Better way: • Define a moveMile instruction as eight move instructions. • Then call moveMile 5 times. • Both programs move the robot the same distance, but the second program will be much easier to read and understand. • In complicated programs, the ability to extend a robot’s vocabulary makes the difference between understandable programs and unintelligible ones. Example

  6. To build a new class of robots, we include a class specification in a new file of our program. • The general form of this specification: public class<new-class-name> extends<old-class-name> { <list-of-new-methods> } Defining New Classes of Robots

  7. Reserved Words and symbols: • public • class • extends • braces { } • We must replace the elements in angle brackets < > appropriately. <new-class-name> what do we call this new type of robot? <old-class-name> what old robot is used to add features to? <list-of-new-methods> list of instructions Specification Details

  8. public void <instruction-name> ( ) { <list-of-instructions> } • The instruction name should specify what the method is intended to do. • The list of instructions should accomplish what its name implies. This is a “block” of code Defining New Methods

  9. public class MileWalker extends Robot { public void moveMile() { move(); move(); move(); move(); move(); move(); move(); move(); } // on two lines due to lack of space // any other methods that need to be // defined would follow } Defining a MileWalker Robot

  10. We indicate the MileWalker inherits all the capabilities of the Robot class • in other words MileWalker knows all about move(), turnLeft(), pickBeeper(), putBeeper(), and turnOff() • Robot is the base class of MileWalker • MileWalker is a derived class of Robot • IS-A relationship • MileWalker IS A Robot Public class MileWalker extends UrRobot

  11. MileWalker.java public class MileWalker extends Robot // note the capital letters for the class name { public void moveMile() { move(); move(); move(); move(); // one line to conserve space move(); move(); move(); move(); } // note the robot name is not used } MileWalkerRunner.java task() { MileWalker lisa = new MileWalker (3, 2, East, 0); // declare a MileWalker robot lisa.moveMile(); // call the new method lisa.pickBeeper(); // call an old method lisa.turnLeft(); lisa.move(); lisa.pickBeeper(); lisa.moveMile(); lisa.turnOff(); }

  12. Remember: A robot is a machine. It has no intelligence. • The robot does not understand what we “mean” when we write a program. • It does exactly what we tell it to. • If we code only 6 move instructions for a mile instead of 8, the robot does not know that we made an error. The Meaning and Correctness of New Methods

  13. Task: Before After Karel needs to climb the stairs and sweep the beepers off. What will be the name of our class of robot? StairSweeper What new methods will we need to define? turnRight() and climbStair()

  14. We use at least two files in creating new robots and robot methods • The main class file which is where the robot is constructed and given its task. We will call it StairSweeperRunner.Java • The main class is defined, the world is accessed, the speed is set • The robot is constructed and receives its task instructions in the task() method • The second file contains the description of the new robot, and the definition of its methods. We will call it StairSweeper.Java • A constructor which describes how we build the robot • And the definitions of the new instructions Robot Program Format

  15. Main Class:StairClimberRunner.java public class StairClimberRunner implements Directions { public static void task() { StairSweeperkarel = new StairSweeper(1,1,East,0); karel.climbStair(); karel.pickBeeper(); // other instructions karel.turnOff(); } // Main entry point public static void main(String[] args) { World.setDelay(50); World.readWorld("stairs.txt"); World.setVisible(); task(); } }

  16. public class StairSweeperRunner implements Directions { // details of the class specification here } • Name of class will be same as the name of the file, with a .java suffix • This class is contained in the file StairSweeperRunner.java • Capitalization counts • Directions is an interface that is is implemented by StairSweeperRunner • In other words StairSweeperRunner fleshes out the details of Directions • Directions has information that a robot needs to navigate in its world • Main has the remaining details specific to the particular task the robot has to perform Class Header

  17. Entry Point // Main entry point public static public void Main(String[] args) { World.setDelay(20); World.readWorld("stairs.txt"); World.setVisible(); task(); } • Every robot application needs to start somewhere, and they will always start with main() in this way • void is a return-type; we will discuss later • We set up the robot world from a data file (has locations of walls, beepers) • We ask the robot to perform the task • There is only one main() in every Java application

  18. Thetask public static void task() { StairSweeper karel = new StairSweeper(1, 1, East, 0); karel.climbStair(); karel.pickBeeper(); // other instructions karel.turnOff(); } We construct the robot by giving it a name and specifying location, direction it is facing, and number of beepers it is carrying. We then provide the set of instructions to the robot.

  19. StairSweeper Class: StairSweeper.java public class StairSweeper extends Robot { // constructor public StairSweeper(int street, int avenue, Direction direction, int howmany) { super(street, avenue, direction, howmany); } //methods public void turnRight() { turnLeft(); turnLeft(); turnLeft(); } public void climbStair() { turnLeft(); move(); turnRight(); move(); } }

  20. Class header: StairSweeper public class StairSweeper extends Robot { } • Name of class will be same as the name of the file, with a .java suffix • This class is specified in the file StairSweeper.java • The StairSweeperrobot inherits information and extends the capabilities of the Robot robot • Everything a Robot can do, a StairSweeper can do • move(), turnLeft(), pickBeeper(), putBeeper(), turnOff() • But a StairSweeper will be able to do more (have more features)

  21. Constructing a new robot public StairSweeper(int street, int avenue, int direction, int howMany) { super(street, avenue, direction, howMany); } • This specifies how a robot is to be constructed • Go back and look at the task() • The instruction new StairSweeper(1, 1, 0, 0);is using this method, known as a constructor. • We are specifying location, direction, and number of beepers • A constructor has the same name as the class. • The super keyword is indicating that this object is to be built the same way as its parent, Robot. • Our robot constructors will always look like this at the beginning.

  22. New robot methods public void turnRight() { turnLeft(); turnLeft(); turnLeft(); } public void climbStair() { turnLeft(); move(); turnRight(); move(); } public is a modifier letting us know that we can access this method from outside the class (in task() for example) Notice that climbStair() can use turnRight() as part of its definition The method headers are known as signatures The signatures of a class are known as the class interface

  23. Problem Solving Process: • Definition of the Problem • Planning the Solution • Implementing the Plan • Analyzing the Solution Designing and Writing Programs

  24. The initial definition of the problem is presented when we are provided figures of the initial and final situations. • The problem must be understood completely before beginning to write a program. 1. Definition of the Problem

  25. Step-Wise Refinement • Write the main task block using any robots and instruction names that seem appropriate. • Write the definitions of the new instruction names used. • Assemble these separate pieces into a complete program. 2. Planning the Solution

  26. Write the code. 3. Implementing the Plan

  27. Did the robot complete the problem correctly as given? 4. Analyzing the Solution

  28. Task: Climb Stairs and remove beepers • Did karel climb the stairs correctly? • Did karel remove all of the beepers?

  29. It is useful to divide a program into a small set of instructions, even if those instructions are executed only once. • New instructions nicely structure programs, and English words and phrases make programs more understandable; they help convey the intent of the program. Advantages of Using New Instructions

  30. Planning mistakes (execution and intent errors): • These happen when we write a program without a well-thought-out plan. • This can waste a lot of programming time. • Usually difficult to fix because large segments of the program have to be modified or discarded. • Careful planning and thorough analysis of the plan can help us avoid planning mistakes. Avoiding Errors

  31. Avoiding Errors Programming mistakes (lexical and syntax errors): These happen when the program is actually written. They can be spelling, punctuation, or other similar errors. If we write the entire program without testing it, we will probably have many errors to correct, some of which may be multiple instances of the same mistake. Writing the program in pieces will both reduce the overall number of errors introduced at any one time and may prevent multiple occurrences of the same mistake.

  32. The robot’s world can be readily changed and we must be able to modify existing programs to keep the robot out of trouble. • It can be much simpler and takes less time to modify an existing program to perform a slightly different task than to write a completely new one. Future Modifications

  33. Suppose we wanted climb to climb 6 stairs instead of 3? How would the program get modified? • Suppose there were 2 beepers on each stair instead of 1? How would the program get modified? New StairClimber

  34. Good programmers are distinguished from bad ones by their ability to write clear and concise programs that someone else can read and quickly understand. • What makes a program easy to understand? Writing Understandable Programs

  35. A good program is the simple composition of easily understandable parts. • Each part of the programs we just wrote can be understood by itself. • Even without a detailed understanding of the parts, the plans that the programs use to accomplish their respective tasks are easy to understand. Writing Understandable Programs

  36. Dividing a program (or large instruction definition) into small, easy to understand pieces is not enough. • We must also make sure to name our new instructions properly. • These names provide a description of what the instruction does. Writing Understandable Programs

  37. When writing a program, we should hand simulate each instruction immediately after it is written, until we are convinced that it is correct. Then we can forget how it works and just remember what it does. • If new instructions are added to a program one at a time, debugging will be easy. The bug will have to lie with the new instruction. • A good rule of thumb is that definitions should rarely exceed five to ten instructions. If it exceeds this limit, then divide it into a set of smaller instructions. New Instructions

  38. If a new instruction can only be executed correctly in a certain situations, then we should include comments in the definition explaining what those conditions are. • Example: An instruction that always picks up a beeper should indicate in a comment where that beeper must appear. New Instructions

  39. public void stepAndFetchIt() // requires a beeper on the next corner in front { move(); pickBeeper(); } Example

  40. moveMile(); moveBackward(); moveKiloMile(); • Pin Setter • Harvest • Baseball game • Send greetings • #5 with five robots • #5 with five robots on streets • # 5 with seventeen robots on avenues • Display time • #9 with two robots • Gardening 3.12 Problems p. 27(p59)

More Related