Introduction to Computer Science Extending Robots’ Vocabularies –New Methods –Top-Down Design –Multiple Robots Unit 2.

Slides:



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

Nested If Statements While Loops
Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The.
Murphy’s Laws. Things are more complex than they seem to be. Things take longer than expected. Things cost more than expected. If something can go wrong,
Chapter 3 Extending the Robot Programming Language.
Lecture Roger Sutton 21: Revision 1.
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.
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.
You ARE NOT ALLOWED To Copy Files!!!. You ARE NOT ALLOWED To Be Told What to Write in the Program.
Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4.
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.
Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3.
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 Inheritance Classes and Methods: Static Methods and Variables Unit 11.
Chapter 1 Program Design
Robot? What’s a Robot? Introducing Karel-the-Robot.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
C++ Crash Course Class 1 What is programming?. What’s this course about? Goal: Be able to design, write and run simple programs in C++ on a UNIX machine.
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.
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Chapter 5 Conditionally Executing Instructions
1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)
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.
Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming.
Karel the Robot A Gentle Introduction to the Art of Programming.
Multiplying Whole Numbers © Math As A Second Language All Rights Reserved next #5 Taking the Fear out of Math 9 × 9 81 Single Digit Multiplication.
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.
Thanks to Dr. Kris Schindler for this (and all Karel the Robot slides)
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
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.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.
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
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.
Introduction to Computer Science
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.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Chapter 3 Extending the Robot Programming Language.
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 – Primitive Instructions
Karel J Robot.
Polymorphism Simple but profound!.
slides courtesy of Eric Roberts
A Gentle Introduction to the Art of Object Oriented Programming
Nested If Statements While Loops
Karel – Primitive Instructions
Presentation transcript:

Introduction to Computer Science Extending Robots’ Vocabularies –New Methods –Top-Down Design –Multiple Robots Unit 2

2- 2 The Basic Language Can Be Clumsy To make a robot turn right, we have to send it 3 turnLeft messages In the robot world there are 8 blocks to the mile. To have a robot go ten miles east, pick up a beeper, then move ten miles north, we would need to have 160 move messages. We think in one language, but must program robots in another.

2- 3 The Solution Give the robot a dictionary of useful methods and definitions Each definition is built from simpler ones that the robot already understands The simplest instructions are the primitive ones that the robot knows Now we can solve problems using instructions natural to our way of thinking.

2- 4 For Example… A new method, turnRight, can be defined as three turnLeft instructions A new moveMile method can be defined as eight move instructions Our beeper picking program can now have 20 moveMiles and 8 moves (in the definition of moveMile), 28 instead of 160. The smaller program is much easier to read and understand. This is important!

2- 5 How do we Get a Robot that Has New Methods? We create a new Class that extends the BasicRobot Class The new Class inherits all of the methods and attributes of the BasicRobot Class, but then adds new methods of its own Then we use the new Class to create instances of robots, with the new methods

2- 6 Inheritance extends Robots created as instances of Class 2 have access to methods 1, 2, and 7 Class 1 method 1 method 2 Class 3 method 5 method 6 Class 2 method 7 Robots created as instances of Class 3 have access to methods 1, 2, 5, and 6

2- 7 Inheritance Class 1 is more general, less specific Classes 2 and 3 have more methods and attributes extends Class 1 method 1 method 2 Class 3 method 5 method 6 Class 2 method 7

2- 8 Class Hierarchy (showing Attributes, not Methods) extends Containers filled material color Boxes length method 6 Cereal Boxes Cylinder extends length width height radius height type of cereal Software Boxes type of software extends Objects made from this class have all the attributes of the classes above them

2- 9 Constructing Objects The objects we create, using a constructor, can be from any of those classes (though the most specific ones might be the most useful) We can make a Cheerios box object or a Corn Flakes box object from the “Cereal Boxes” class But we might also make just a “box” object from the Boxes class

2- 10 The Details, Defining a new Method, moveMile, inside a new class class MileWalker extends BasicRobot { void moveMile { move; move; move; move; move; move; move; move; } }

2- 11 class MileWalker extends BasicRobot { void moveMile { move; move; move; move; move; move; move; move; } } The Details, Defining a new Method, moveMile, inside a new class New reserved words, class, extends, void

2- 12 Visually, What Have We Got? move turnLeft pickBeeper putBeeper turnOff BasicRobot x-location y-location direction num-beepers move turnLeft pickBeeper putBeeper turnOff moveMile MileWalker x-location y-location direction num-beepers extends

2- 13 Block Structuring Block structuring makes one big instruction out of a sequence of smaller ones — place a sequence of instructions between the delimiters { and }: { ; } The entire block represents one method.

2- 14 But who is the “move” a Message To? The messages will be sent by an instance of the MileWalker class to itself ! class MileWalker extends BasicRobot { void moveMile { move; move; move; move; move; move; move; move; } } We could also write “this.move”, but leaving it off is legal and simpler.

2- 15 Robot Talks to Himself Using a constructor, we make an instance of the MileWalker class, called steve: MileWalker steve = new MileWalker(2, 3, East, 0) Now, we’ve got a new initialized object named steve We send steve a moveMile message: steve.moveMile; steve receives the message, and sends himself 8 “move” messages

2- 16 Finally, turnRight class RightTurner extends MileWalker { void turnRight { turnLeft; turnLeft; turnLeft; } } Yes, Virginia, we can have multiple levels of inheritance—and we can redefine ( override) an existing definition in an inheriting class. BasicRobot MileWalker RightTurner extends

2- 17 Overriding Inherited Methods (bizarre example) Let’s say we wanted a new Class of Robot, Spinner, who turned around twice when it was sent the turnRight message class Spinner extends RightTurner { void turnRight { turnLeft; turnLeft; turnLeft; turnLeft; turnLeft; turnLeft; turnLeft; }

2- 18 The Meaning and Correctness of New Methods Robots do not understand what methods are supposed to accomplish; robots are quite literal: void turnRight { turnLeft; turnLeft; } This is an intent error. A new instruction can also cause an error shutoff.

2- 19 Meaning and Correctness The definition on the previous slide is a perfectly legal method, but fundamentally wrong. The name says what the method is intended to do. The definition says how the method does what the name implies. The two had better match; otherwise, one or both should be changed.

2- 20 Simulating Instructions When simulating newly-defined methods, make sure to simulate literally what the method does, and not take a shortcut and simulate what the defined method’s name means. Robots can’t take the shortcut; neither can you.

2- 21 Defining New Instructions in a Program Initial Situation Final Situation

2- 22 Defining New Instructions in a Program

2- 23 Defining New Instructions in a Program

2- 24 Defining New Instructions in a Program

2- 25 Defining New Instructions in a Program

2- 26 Defining New Instructions in a Program

2- 27 Defining New Instructions in a Program

2- 28 Defining New Instructions in a Program

class StairSweeper extends BasicRobot { void turnRight { turnLeft; turnLeft; turnLeft; } void climbStair { turnLeft; move; turnRight; move; } } main { StairSweeper larry = new StairSweeper(2, 1, East, 0); larry.climbStair; larry.pickBeeper; larry.climbStair; larry.pickBeeper; larry.climbStair; larry.pickBeeper; larry.turnOff; }

Importing Class Definitions Instead of putting all these things together in one file, we could break them up as follows: class StairSweeper extends BasicRobot { void turnRight { turnLeft; turnLeft; turnLeft; } void climbStair { turnLeft; move; turnRight; move; } } A file called StairSweeper.r import StairSweeper; main { StairSweeper larry = new StairSweeper(2, 1, East, 0); larry.climbStair; larry.pickBeeper; larry.climbStair; larry.pickBeeper; larry.climbStair; larry.pickBeeper; larry.turnOff; } A file called main.r

2- 31 The Dictionary Declaration of a new class (and its new methods) is placed before the main block. This area is called the dictionary; each definition is called a dictionary entry The definition of BasicRobot is “built-in” The order of dictionary entries is not important

2- 32 Memory fades… The Controller does not memorize dictionary entries forever When it is turned on, the only thing it remembers is the BasicRobot class and reserved words Each program has to include a full set of definitions for all new instructions it uses

2- 33 An Ungrammatical Program class RightTurner extends BasicRobot { void turnRight turnLeft; turnLeft; turnLeft; } } main { RightTurner ted = new RightTurner(2, 2, North, 0); ted.move; ted.turnRight; ted.turnOff; }

2- 34 The Controller Finds a Mistake When the Controller is being read a program it is constantly looking for lexical and syntactic errors The Controller discovers syntactic errors by checking for proper grammar and punctuation

2- 35 class RightTurner extends BasicRobot { void turnRight turnLeft; turnLeft; turnLeft; } } main { RightTurner ted = new RightTurner(2, 2, North, 0); ted.move; ted.turnRight; ted.turnOff; } An Ungrammatical Program The next thing should have been a delimiter, { Forgetting to use delimiters can lead to syntactic errors.

2- 36 Programming by Top-Down Design How can we naturally write a set of methods that are correct, simple to read, and easy to understand? We do not first write all the methods and then write the main program block (how can we know ahead of time which instructions will be needed?).

2- 37 Top-Down Design First write the main program block using any methods we want, then write the definitions of these methods in a new class Then, put it all together

2- 38 Top-Down Design The process can continue, with methods used in the dictionary causing us to define more methods (above them) Execution Block Dictionary }

2- 39 The Harvest Task Initial Situation Final Situation

2- 40 The (High-Level) Program main { Harvester bill = new Harvester(2, 2, East, 0); bill.move; bill.harvest2Furrows; bill.positionForNext2; bill.harvest2Furrows; bill.positionForNext2; bill.harvest2Furrows; bill.move; bill.turnOff; } New Methods: harvest2Furrows positionForNext2

2- 41 One of the New Methods void harvest2Furrows { harvestAFurrow; goToNextFurrow; harvestAFurrow; } New Methods:  harvest2Furrows positionForNext2 harvestAFurrow goToNextFurrow Subtasks generate sub-subtasks: Remember: this method will be inside a new Class, of which the bill robot will be an instance The object created from this class will be sending messages to himself...

2- 42 Another New Method void harvestAFurrow { pickBeeper; move; pickBeeper; move; pickBeeper; move; pickBeeper; move; pickBeeper; } New Methods:  harvest2Furrows positionForNext2  harvestAFurrow goToNextFurrow Assumption: Robot is next to first beeper in a furrow, rest of the furrow in front of him

2- 43 Yet Another void goToNextFurrow { turnLeft; move; turnLeft; } New Methods:  harvest2Furrows positionForNext2  harvestAFurrow  goToNextFurrow Assumption: Robot is facing east and wants to go one block north, then turn to face west

2- 44 And Finally… void positionForNext2 { turnRight; move; turnRight; } New Methods:  harvest2Furrows  positionForNext2  harvestAFurrow  goToNextFurrow turnRight Assumption: Robot is facing west on the corner of last harvested beeper in a furrow

2- 45 Now Assemble into Complete System class Harvester extends BasicRobot { void turnRight {... } void positionForNext2 {... } void goToNextFurrow {... } void harvestAFurrow {... } void harvest2Furrows {... } } main { Harvester bill = new Harvester(2, 2, East, 0); bill.move; bill.harvest2Furrows; bill.positionForNext2; bill.harvest2Furrows; bill.positionForNext2; bill.harvest2Furrows; bill.move; bill.turnOff; }

2- 46 Verify the Program Verification through simulation is still necessary “Programs are assumed to be guilty of being wrong until they are proven correct.”

2- 47 Consider the Methods Your Objects Should Have Your objects can have many small methods, even if the new methods are only executed once in your program! Early decisions that are made are the most important—they establish the structure of the rest of the program (try several high-level plans). Use words and phrases to convey the intent of the program…

2- 48 Object Oriented Analysis and Design In a real (non-robot) system, you have to decide what the objects are, then figure out what methods they should have There are entire courses on how to analyse a system, decide what the objects are, and design them effectively There is both art and science in good object oriented programming

2- 49 How to Break It Up We could easily solve the problem of harvesting the field by using three separate robot objects, all instances of the class Harvester. The main block will send messages to them one after another.

Three Separate Robots Harvesting class Harvester extends BasicRobot { void turnRight {... } void positionForNext2 {... } void goToNextFurrow {... } void harvestAFurrow {... } void harvest2Furrows {... } } A file called Harvester.r import Harvester; main { Harvester john = new Harvester(2, 2, East, 0); Harvester paul = new Harvester(2, 4, East, 0); Harvester george = new Harvester(2, 6, East, 0); john.move; john.harvest2Furrows; john.turnOff; paul.move; paul.harvest2Furrows; paul.turnOff; george.move; george.harvest2Furrows; george.turnOff; } A file called main.r

2- 51 Objects Sending Messages to Other Objects We could have a single robot that, when sent a move message, moves himself and send two other move messages to two other “helper” robots The new class overrides the definition of move so that it is defined as –move myself –move helper 1 –move helper 2 Same thing for pickBeeper and turnLeft

2- 52 Objects Controlling Other Objects Send first robot move message

2- 53 Objects Controlling Other Objects He moves

2- 54 He sends a move message to first helper First helper moves Objects Controlling Other Objects

2- 55 He sends a move message to second helper Second helper moves Objects Controlling Other Objects

2- 56 Send first robot pickBeeper message Objects Controlling Other Objects

2- 57 He picks up beeper Objects Controlling Other Objects

2- 58 He sends a pickBeeper message to first helper First helper picks up beeper Objects Controlling Other Objects

2- 59 He sends a pickBeeper message to second helper Second helper picks up beeper Objects Controlling Other Objects

2- 60 First robot is a new class, where we have overridden the definitions of move, pickBeeper, etc. Helper robots can be plain BasicRobots Objects Controlling Other Objects How does the first robot define his own, simple, “move”, when he is overriding “move”?

2- 61 The Paper Retrieving Task main { PaperBoy lou = new PaperBoy(4, 3, West, 0); lou.goToDoor; lou.exitHouse; lou.getPaper; lou.returnToDoor; lou.enterHouse; lou.goBackToBed; lou.turnOff; }

2- 62 Writing Understandable Programs A good program is the simple composition of easily understandable parts We must make sure to name our new methods properly –the names describe how tasks are accomplished –imagine the previous program with methods named “firstMethod” and “doItNow”

2- 63 Defined Methods Make a Program Easier to Debug/Verify Defined methods can be independently tested — verified, then remember just what the object does, not how it does it Defined methods impose a structure to help locate bugs Hiding the implementation of an object’s methods is a central concept in object oriented programming

, plus or minus 2 The human brain can focus on a limited amount of information at one time The ability to ignore details that are no longer relevant is useful for program writing and debugging

2- 65 Build a House Task Initial Situation Final Situation

2- 66 Good OOP Means Seeing Appropriate Decomposition Initial Situation Final Situation Areas of Responsibility

2- 67 Many Choices are Possible This breakdown into two objects is only one possible solution Many others exist Good design has many aspects, and we’ll look at this in greater detail throughout the course: –Considering the objects that comprise a system –Considering the methods that each object (class) should have

2- 68 Keep them Small Use multiple classes, as appropriate for the system Methods should rarely exceed five to eight instructions If they are longer, break them up That includes the main execution block WRITE MANY SMALL, WELL-NAMED METHODS, RATHER THAN A FEW OVERSIZED METHODS.

2- 69 Imagine All the Programs… Imagine the harvesting program without defined methods How easy to convince someone it is correct? How easy to locate and correct an error (like a missing instruction)? How hard to change slightly (make 3 more beepers per furrow)?