250 likes | 688 Views
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have to have (26+) * 8 move() statements? Wouldn’t it be nice to tell a robot to run a marathon Karel.runMarathon();
E N D
Extending the Robot Programming Language • In the Robot world 1 mile = 8 blocks • Suppose we want a robot to run a marathon (26+ miles)? • Does our program have to have (26+) * 8 move() statements? • Wouldn’t it be nice to tell a robot to run a marathon • Karel.runMarathon();
Creating a More Natural Programming Langauge • We think in one language, but must program robots in another • But Karel-Werke has given robots the ability to learn the definitions of new methods • We specify new classes of robots • these classes provide specifications for new robot instructions • Karel-Werke uses class descriptions to create the new robots
A Marathon-running Robot • If we specify a class of robot to run a marathon • we can define a moveMile() instruction as eight move() messages • we can reduce the size of our program by (26+) * 8 / 8 or down to 26+ instructions • Can we do better? • we can specify more than one new instruction per new class of robots • what other instructions could we define to further simplify the problem?
Defining New Classes of Robots • To specify a new class of robots, we include a class specification in a new file of our program • The general form of this specification: class <new-class-name> extends <old-class-name> { <list-of-new-methods> }
Specification Details • Reserved Words and symbols • class • extends • braces { } • We must replace the elements in angle brackets < > appropriately • <new-class-name> what do we call this new style robot? • <old-class-name> what old robot to add features to? • <list-of-new-methods> list of new features
Naming Things • In developing the names for robots and new methods • any uppercase and lowercase letters {A..Z, a..z}, digits {0..9}, and underscore { _ } can be used • unique name to the program • does not match any reserved words • must begin with a letter • typically upper case for a class • lower case for a method or instruction
Specifying a Marathon Robot class MarathonRobot extends ur_Robot { void moveMile() { // instructions omitted for now } // other instructions }
extends ur_Robot? class MarathonRobot extends ur_Robot • we indicate the MarathonRobot inherits all the capabilities of the ur_Robot class • in other words MarathonRobot knows all about move(), turnLeft(), pickBeeper(), putBeeper(), and shutOff() • ur_Robot is the parent class of MarathonRobot • MarathonRobot is a sub-class of ur_Robot
Defining the new methods • As we declare a new robot class we need to define all the new instructions introduced • We will start by defining moveMile() and testing it • Download MarathonRobot Demo
Defining a new method: Details • It is public so that it can be used outside of the class (in this case, by the Main class) • The MarathonRobot belongs to the kareltherobot package as does everything we will do • The block of the method is like a main task block • messages not prefaced by the name of any particular robot • in the main task block we need to tell a particular robot to perform an instruction • here a robot of the MarathonRobot class will carry out this instruction, when it receives a moveMile() instruction • the robot will carry out the instruction list itself, so a robot name is not required here… • could have used a special reserved word like this, but we do not...
Meaning of New Methods • A robot does not “understand” what we “mean” • A class declaration is a description of how to construct robots of this class • Each robot stores the definitions so that it can respond to instructions it receives
Correctness of New Methods • If we define a new instruction, moveMile(), it does not mean, the robot actually moves one mile • Even without lexical or syntax errors • Suppose we have six move instructions instead of eight? • Suppose there is a wall directly in the robots path? • When simulating a robot’s instruction, make sure we follow the instructions given, not just assume that the name of the method is an accurate description
Defining new methods in a program • Download Stair Climbing Robot Demo • Verify this program is correct by tracing the code before running it
Robot Program Format • 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 • 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 • A constructor which describes how we build the robot • And the definitions of the new instructions
Main Class public class Main implements Directions { public static void task() { Stair_Sweeper Karel = new Stair_Sweeper(1, 1, East, 0); Karel.climbStair(); Karel.pickBeeper(); // other instructions … Karel.turnOff(); } // Main entry point static public void main(String[] args) { World.setDelay(20); World.readWorld("stairs.txt"); task(); } }
Class Header: Main public class Main 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 Main.java • Capitalization counts • Directions is an interface that is is implemented by Main • In other words Main 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
Entry Point // Main entry point static public void main(String[] args) { World.setDelay(20); World.readWorld("stairs.txt"); 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 will ignore the modifiers in italics for now other than to say they give us access to the robot methods • We set up the robot world • We ask the robot to perform the task • There is only one main() in every robot program
The task public static void task() { Stair_Sweeper Karel = new Stair_Sweeper(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
StairSweeper class class Stair_Sweeper extends ur_Robot { // constructor public Stair_Sweeper(int street, int avenue, int direction, int howmany) { super(street, avenue, direction, howmany); } //methods public void turnRight() { turnLeft(); turnLeft(); turnLeft(); } public void climbStair() { turnLeft(); move(); turnRight(); move(); } }
Class header: StairSweeper class Stair_Sweeper extends ur_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 ur_Robot robot • Everything a ur_Robot can do, a StairSweeper can do • move(), turnLeft(), pickBeeper(), putBeeper(), turnOff() • But a StairSweeper will be able to do more (have more features)
Constructing a new robot public Stair_Sweeper(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 Stair_Sweeper(1, 1, East, 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, ur_Robot • Our robot constructors will always look like this at the beginning
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 New robot methods
Advantages of using new instructions • Structure programs • Programs become easier to understand and read • Lead to fewer errors • Enable future modifications • Debugging programs is easier • New instructions can be tested independently • New instructions impose structure, which makes it easeir to find bugs
Writing Understandable Programs • A composition of easily understandable parts • Name new instructions properly, providing a description • Self-documenting programs • Good instructions have a reasonable length (5 – 10 instructions)
Case Study: Karel Harvester • Download Karel Harvester Demo