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

Slides:



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

1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Copyright, Joseph Bergin
Chapter 3 Extending the Robot Programming Language.
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,
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.
1 Inheritance in Java Behind the scenes: new Objects from old.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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 Inheritance Classes and Methods: Static Methods and Variables Unit 11.
Computer Science I Inheritance Professor Evan Korth New York University.
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.
Chapter 5 Conditionally Executing Instructions
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 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming.
Karel the Robot A Gentle Introduction to the Art of Programming.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3.
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 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.
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.
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()
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
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,
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Inheritance ndex.html ndex.htmland “Java.
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 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
Copyright © 2008 by Helene G. Kershner
Ch.3 Classes & Stepwise Refinement
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.
Polymorphism and Observers
Ch.3 Classes STEP 1 Define a new class of robot (see next slide)
Karel – Primitive Instructions
Karel J Robot Chapter 4 B.
Karel J Robot.
Road Map Inheritance Class hierarchy Overriding methods Constructors
Polymorphism Simple but profound!.
Karel J Robot OOP approach to learning computer science
Karel J Robot OOP approach to learning computer science
Ch.3 Classes & Stepwise Refinement
Unit 1 Lab16.
Object References a.k.a. variables
Ch.3 Classes & Stepwise Refinement
Ch.3 Classes & Stepwise Refinement
More on Creating Classes part 3
Karel – Primitive Instructions
Presentation transcript:

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

2 Compare MileWalker to the basic UrRobot: 1)What is different? 2)What is the same? MileWalker can move 8 blocks MileWalker can move one block, turn left, pick up a beeper, put down a beeper, and turn off (just like UrRobot) In OOP terminology, it would make sense for MileWalker to inherit the capabilities of UrRobot.

3 STEP 1 Define a new class of robot (see next slide): When designing a new class (whether it’s a type of robot, car, bank account, etc.), the first question we are asking ourselves is “What can I steal?” !! In other words, one big benefit of OOD is the concept of code- reuse. There is no need to reinvent the wheel. (by the way, that doesn’t mean to copy your friend’s lab!! ) Also, what is different? We need to create new methods to reflect the differences.

4 Inserting MileWalker into the Inheritance Hierarchy UrRobot MileWalker move() moveMile() turnLeft() pickBeeper() putBeeper() turnOff() If you have an object (say, bob) of type MileWalker, what methods are available to bob? What if bob were of type UrRobot? the is-A relationship: a MileWalker is-A UrRobot

5 import kareltherobot.*; public class MileWalker extends UrRobot { public MileWalker ( int st, int av, Direction dir, int beeps ) { super(st, av, dir, beeps); } public void moveMile( ) { move(); move(); } } note: no object names preceding methods (why not?) - let’s look at client code to see why superclass (or parent class) subclass constructor invokes superclass’ constructor

6 STEP 2 Write application (client) to use your new object class (client is also known as a driver program). This should be a separate class from the object class. import kareltherobot.*; public class MileWalkerTester implements Directions { public static void main(String args[]) { MileWalker bob = new MileWalker(2, 1, East, 0); bob.moveMile(); // new instruction bob.move();// inherited instruction bob.turnOff();// inherited instruction } This client program is similar to your previous programs. Instead of constructing a UrRobot type of robot, we are constructing a MileWalker robot.

7 (don’t sweat this – it’s not computer science) These 4 method invocations may be necessary and may be placed first within the main() method of the client (driver) program: –World.reset(); –World.readWorld(“ch2_6_wld.txt"); –World.setDelay(50); –World.setVisible(true); –World.showSpeedControl(true); –Alternatively, you can place them in a static block (no need to understand what a “static block” is) within your driver class. A static block runs before the main() method runs.

8 You should end up with two classes: –an object class that defines the new type of robot –a client class which is used to test the new robot The client program should test several of the methods in the new robot class: –Make the robot move a single block, a mile (8 blocks), turn left, etc. Convention: class names begin with a Capital Letter, method names begin with a lowercase letter – if an identifier combines several words, use the “camelCase” technique. (It is important to follow convention.) We’ll now demo the whole process in jGRASP and look at MileWalker and MileWalkerTester.

9 Create a BetterRobot class –Create methods to: turnRight(), - turns right (does not move) turnAround(), - turns backwards (does not move) stepBackward(), - moves backwards and remains in same direction moveMile(), - 8 corners moveKiloMile() - 80 corners Any other methods that might be helpful?? Create a small client program named BetterRobotTester to test the BetterRobot class –Construct the new robot –Move the robot around the screen using inherited methods or the new methods created above