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.

Slides:



Advertisements
Similar presentations
Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when.
Advertisements

1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
1 of 3 Karel Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
Copyright, Joseph Bergin
You ARE NOT ALLOWED To Copy Files!!!. You ARE NOT ALLOWED To Be Told What to Write in the Program.
1 Inheritance in Java Behind the scenes: new Objects from old.
CMPUT 101 Lab # 2 September 17, :00 – 16:50.
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.
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.
CPSC150 Abstract Classes and Interfaces Chapter 10.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
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.
Karel JRobot Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
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.
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)
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.
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 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
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.
1 Ch Object References a.k.a. variables Teams of Robots (e.g.) –Could have 1 robot harvest 6 rows (we’ve seen that) –Could have 3 robots each.
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()
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.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
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.
1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)
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.
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 J Robot.
Karel – Primitive Instructions
Karel J Robot Chapter 4 B.
Karel J Robot.
CS 106A, Lecture 2 Programming with Karel
Polymorphism Simple but profound!.
Karel J Robot OOP approach to learning computer science
Algorithm The key is the step-by-step instructions.
Karel the Robot – Making Decisions
A Gentle Introduction to the Art of Object Oriented Programming
Karel J Robot OOP approach to learning computer science
Object References a.k.a. variables
Algorithm The key is the step-by-step instructions.
CS2013 Lecture 7 John Hurley Cal State LA.
Karel – Primitive Instructions
Presentation transcript:

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 to tackle the problem Three robots, each picking two rows Six robots each picking one row – Download HarvesterW3.zip demo projectHarvesterW3.zip

Names of Robots as “references” We do not need three different names to refer to three different robots – We can use one name to refer to three robots in sequence – Name of robots are “references” public static void task() { Harvester Karel = new Harvester(2, 2, East, 0); Karel.pickTwoRows(); Karel.turnOff(); Karel = new Harvester(4, 2, East, 0); Karel.pickTwoRows(); Karel.turnOff(); Karel = new Harvester(6, 2, East, 0); Karel.pickTwoRows(); Karel.turnOff(); }

Declaration versus Robot Creation We only declared a Robot name once Harvester Karel = – Karel is a name that refers to Harvester robots We created three robots = new Harvester(2, 2, East, 0); – Creating a Harvester robot at 2, 2, facing East, with 0 beepers We could better illustrate this in code: Harvester Karel = null; Karel = new Harvester(2, 2, East, 0) – null : a reference to a robot is not referring to any real robot

Similar Tasks We want to do several tasks, which are very similar Task: We want to lay down beepers in a field with 5 rows, four columns. Odd numbered rows will have two beepers on each corner, even numbered rows will have three beepers on each corner

Different Robots, Similar skills TwoRowLayer vs ThreeRowLayer Very similar, differ only in one way Capture the commonality in an abstract class – A template for the construction or more specific robots – Download BeeperLayer.zip demo projectBeeperLayer.zip This is polymorphism: two objects (robots) receiving the same instruction, will behave differently – The object receiving the instruction determines what is done when the message is received

Single robot vs. team of robots We could build a robot to solve every possible task, a super-duper robot. Should we? – This results in a large program – Large programs are hard to build and harder to modify – Large programs are hard to understand Better to build small robot classes with a single, simple to understand purpose – With small robot classes we can choose a set of robots to solve much of a complex task, and write other simple classes to perform the rest If we design our classes well, our robots will be trustworthy

Choreographers Let one robot coordinate the actions of some others Need two different kinds of robots – One a Choreographer, because it directs the others – The others can be standard ur_Robots The Choreographer will set up the others so that they mimic the actions of the others – Download Choreographer.zip demo projectChoreographer.zip

Choreographer details Choreographers need to know the names of the other robots, so we make these private to the Choreographer – Helpers to the robot class being declared, but not accessible to other robots or to the main task block We will also overwrite (redefine) the inherited primitive instructions of the Robot class, so that all the other private robots know what to do – move(), turnLeft(), pickBeeper(), putBeeper(), turnOff()

Doing Arithmetic with Robots Computers manipulate numbers well We want to teach robots to manipulate numbers We could represent a value, say 24, by putting 24 beepers on a corner Or, we could use two corners, the first will have 2 beepers, and the second will have 4 beepers

Representation for robot addition To add two digits we could set up a world to look like this (but we will start the columns on 2 nd Avenue, not 1 st ) Download Adder.zip demoAdder.zip

The robot addition classes The Adder robot will utilize two helper robots – The first to see if a carry is necessary – The second will actually do the carry if necessary These robots will be created by a parent, and will need to find it in order to help it, we will start with a super (parent) class that provides the finding method: called Finder – All other classes ( Adder, Carrier, Checker ) will be subclasses of Finder) – Actually, we are not going to create any Finder robots

The robot addition classes Finder : – the parent superclass; – a convenient place to place methods common to all the other classes Adder : – Creates helpers; – Picks up all beepers north of it – Returns to 1 st Street – Deposits all of the beepers there

The robot addition classes Carrier : – Quite simple – Takes another beeper to adjacent column if necessary – Given an infinite number of beepers Checker : – Does all of the interesting work – Are there 10 or more beepers collected by Adder? Yes, return true No, return false – Actually it has to check for multiples of 10 – Also, if it finds less than 10 it must leave them on the corner

Polymorphism: Design? When is it appropriate to design a new class, and when should we modify or add to an existing class? – To really be answered as we continue in the course Guidelines: – Modify If a class contains errors or omissions, modify it Omissions mean there is some action or predicate needed to complete the basic functionality of the class – New class If we have a useful class, but we need new, specialized functionality, then build a new class as a subclass Sometimes a class is what we want, but one or two methods may need to be modified