Chapter 3 Extending the Robot Programming Language.

Slides:



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

Chapter 3 Extending the Robot Programming Language.
1 Inheritance in Java Behind the scenes: new Objects from old.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Karel The Robot In the beginning… software. Karel the Robot  All robots are controlled by software  Artificially intelligent robots that can “think”
Conditionals How do we solve tasks in which every particular of a task is not specifically known? – A robot needs the ability to survey its immediate environment.
Polymorphism Are there different ways to solve the Harvester problem? – Robot teams – instead of one robot to solve a problem, let’s get a team of robots.
Introduction to Computer Science Extending Robots’ Vocabularies –New Methods –Top-Down Design –Multiple Robots Unit 2.
Robot? What’s a Robot? Introducing Karel-the-Robot.
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.
Object Oriented Software Development
Chapter 5 Conditionally Executing Instructions
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Classes begin with capital letters (i.e. UrRobot). Methods, objects, and variable names begin with lower case (camelCase) Use indentation to line up.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
The Java Programming Language
Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming.
Karel the Robot A Gentle Introduction to the Art of Programming.
Programming Errors Lexical errors – occur whenever Karel reads a word that is not in his vocabulary. Example in English: We are asking directions and instead.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
15-100: Introduction to Programming w/ Java * Ananda Gunawardena -- Lecture – School of Computer Science – Phone : (x81559) – Office: Wean Hall.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4.
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Compiler Errors Syntax error Lexical Can not resolve/find symbol Can not be applied Execution error Oh wait, a run time error Intent error It ran, but.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Karel J. Robot Chapter 6 Instructions That Repeat.
INTRODUCTION TO ROBOTICS Part 5: Programming
Karel – Primitive Instructions
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
In the beginning… software
Eric Roberts and Jerry Cain
Copyright © 2008 by Helene G. Kershner
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Copyright © 2008 by Helene G. Kershner
karel_part4_functions_2
Karel – Primitive Instructions
Karel J Robot.
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
CS 106A, Lecture 2 Programming with Karel
Polymorphism Simple but profound!.
Chapter 4 void Functions
Programming Fundamentals (750113) Ch1. Problem Solving
Programming Fundamentals (750113) Ch1. Problem Solving
Chapter 1: Computer Systems
CHAPTER 2 Context-Free Languages
slides courtesy of Eric Roberts
A Gentle Introduction to the Art of Object Oriented Programming
Ch.3 Classes & Stepwise Refinement
How to Run a Java Program
Programming Fundamentals (750113) Ch1. Problem Solving
Object References a.k.a. variables
Programming Fundamentals (750113) Ch1. Problem Solving
Karel – Primitive Instructions
Presentation transcript:

Chapter 3 Extending the Robot Programming Language

New Classes of Robots  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.

Dictionary-Like  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.

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

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

Defining New Classes of Robots  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: class extends { }

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

Defining New Methods void ( ) { }  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 a MileWalker Robot class MileWalker extends ur_Robot { void moveMile() { move(); move(); }// on two lines due to lack of space //any other methods that need to be // defined would follow }

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

Putting It Together class MileWalker extends UrRobot // note the capital letters for the class name { void moveMile() { move(); move(); move(); move(); // one line to conserve space move(); move(); move(); move(); } // note the robot name is not used } 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(); }

The Meaning and Correctness of New Methods  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.

Defining New Methods In A Program

Task:  What will be the name of our class of robot?  StairSweeper  What new methods will we need to define?  turnRight() and climbStair()

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. We will call it main.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

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

Class Header 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 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

The task 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

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

Class header: StairSweeper class StairSweeper 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 StairSweeper robot 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 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, 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.

New robot methods public void turnRight() { 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

Trace  Let’s trace the program to see if it completes the task correctly.

Trace  Open KarelStairSweeper demo project.

Designing and Writing Programs Problem Solving Process: 1.Definition of the Problem 2.Planning the Solution 3.Implementing the Plan 4.Analyzing the Solution

1. Definition of the Problem  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.

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

3. Implementing the Plan  write the code

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

Task: Harvest a Field

Advantages of Using New Instructions  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.

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

Future Modifications  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.

New Harvester  Suppose we wanted to harvest 8 rows of beepers instead of 6? How would the program get modified?  Suppose there were 8 beepers in a row instead of 5? How would the program get modified?

Writing Understandable Programs  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  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  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.

New Instructions  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  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.

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

Problems p moveMile(); moveBackward(); moveKiloMile(); 2.Pin Setter 3.Harvest 4.Baseball game 5.Send greetings 6.#5 with five robots 7.#5 with five robots on streets 8.# 5 with seventeen robots on avenues 9.Display time 10.#9 with two robots 11.Gardening