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.

Slides:



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

Introduction to Java.
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The.
Chapter 3 Extending the Robot Programming Language.
1 Karel J Robot-Lesson 3 NTSD-Mr. Moon Karel J Robot Lesson 3 A Gentle Introduction to the Art of Object-Oriented Programming in Java.
IT151: Introduction to Programming
1 Inheritance in Java Behind the scenes: new Objects from old.
Karel the Robot -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Week 2 Recap CSE 115 – Spring Object Oriented Program System of objects that communicate with one another to solve some problem.
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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Robot? What’s a Robot? Introducing Karel-the-Robot.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
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.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
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.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
Thanks to Dr. Kris Schindler for this (and all Karel the Robot slides)
Introduction to programming in the Java programming language.
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.
1 Note: Original slides provided by and modified for Mr. Heath’s AP Computer Science A classwww.apComputerScience.com.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
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.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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()
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
1 Ch. 3 Ch.3 Classes & Stepwise Refinement STEP 1 Define a new class of robot (see next slide) When designing a new class (whether that’s robots, cars,
By : Robert Apeldorn. What is OOP?  Object-oriented programming is a programming paradigm that uses “objects” to design applications and computer programs.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
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.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Chapter 3 Extending the Robot Programming Language.
Karel – Primitive Instructions
Copyright © 2008 by Helene G. Kershner
University of Central Florida COP 3330 Object Oriented Programming
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
Polymorphism and Observers
Karel – Primitive Instructions
Karel J Robot.
A Gentle Introduction to the Art of Object Oriented Programming
Ch.3 Classes & Stepwise Refinement
Unit 1 Lab16.
Karel – Primitive Instructions
Presentation transcript:

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 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 style robot? – what old robot to add features to? – 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 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 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(); } 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 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 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

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

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 Download Karel Harvester Demo