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 this specific 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 Chapter 6.
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.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
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.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
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.
Abstraction, Inheritance, and Polymorphism in Java.
Chapter 5 Conditionally Executing Instructions
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.
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.
Chapter 5.  We’ve been using UrRobot – knows only 5 things  Let’s now use a new type of Robot: Robot!  i.e. – public class MileMover extends Robot.
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()
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,
AP Computer Science A – Healdsburg High School 1 Unit 2 - Object-Oriented Programming - Example.
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.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Lecture 11 Instructor: Craig Duckett Instance Variables.
1/28/2008ITK 1681 An enhanced robot Robot int street int avenue Direction direction ThingBag backback Robot(City aCity, int aStreet, int anAvenue, Direction.
Chapter 3 Extending the Robot Programming Language.
Karel – Primitive Instructions
Interface, Subclass, and Abstract Class Review
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.
Polymorphism Simple but profound!.
Karel J Robot OOP approach to learning computer science
A Gentle Introduction to the Art of Object Oriented Programming
Karel J Robot OOP approach to learning computer science
Ch.3 Classes & Stepwise Refinement
Object References a.k.a. variables
Ch.3 Classes & Stepwise Refinement
Inheritance and Polymorphism
Ch.3 Classes & Stepwise Refinement
Presentation transcript:

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

2 Be efficient! starts off facing East Design a robot class (called StairClimber, method called climbStair) that would be conducive to solving the following diagrammed situation (robot should climb and pick up all beepers – always 3 beepers/stair) Also, different clients need to be able to climb different numbers of stairs: When designing, keep in mind everything we’ve been discussing.

3 You want to do something that is not already defined in another class And you also might want to use functionality already programmed in other classes. If so, use inheritance. Why reinvent the wheel? Use Abstraction when designing your class – free your mind from the irrelevant and work on the relevant! –Ex. If I’m going to write a program to have a robot climb stairs in several buildings, I’m going to use the StairClimber class so I can call climbStair() – I can work on a bigger/better/harder problem and free my mind from the irrelevant details of taking a step and picking beepers

4 You get stuff for free! You can be lazy (but smart)! Use things without knowing/caring how they work! Localize changes to one class/method (encapsulation – data and methods are in the same class) StairClimberMileWalkerHarvesterDiamondPlanter BetterRobot UrRobot

5 Remember MileWalker? Suppose a MileWalker can only move one mile. Every time you send it the move() command, you want it to move a mile. How would you do that? In this case you can redefine what the move() method does.

6 import kareltherobot.*; public class MileWalker extends UrRobot { public MileWalker ( int st, int av, Direction dir, int beeps ) { super(st, av, dir, beeps); } public void move( ) { super.move(); } } The move() method is being redefined. super.move() tells the robot to perform the move() method from the superclass (in this case UrRobot). This is called Polymorphism and we will look at this further in the next chapter.

7 How many times does each robot below move? public class MysteryBot1 extends UrRobot { /* constructor not shown */ public void step1() { move(); } public void move() { super.move(); super.move(); } } public class MysteryBot2 extends MysteryBot1 { /* constructor not shown */ public void step1() { move(); } public void move() { super.move(); super.move(); } } MysteryBot1 joann = new MysteryBot1(10, 10, North, 0); joann.step1(); // where is the robot now? MysteryBot2 bobby = new MysteryBot2(10, 10, North, 0); bobby.step1(); // where is the robot now? (12, 10) facing North (14, 10) facing North

8 Give the state (location and Direction) of each robot below public class ABetterBot extends UrRobot { /* constructor not shown */ public void step1() { move(); } public void step2() { turnLeft(); } public class AnEvenBetterBot extends ABetterBot { /* constructor not shown */ public void step1() { move(); super.step1(); step2(); } public void step2() { turnLeft(); super.step2(); } } ABetterBot ucla = new ABetterBot(10, 10, North, 0); ucla.step1(); // where is the robot now? AnEvenBetterBot usc = new AnEvenBetterBot(10, 10, North, 0); usc.step1(); // where is the robot now? (11, 10) facing North (12, 10) facing South

9 Give the state (location and Direction) of each robot below public class StepperBot extends UrRobot { /* constructor not shown */ public void step1() { move(); step2(); } public void step2() { turnLeft(); } } public class MoreStepperBot extends StepperBot { /* constructor not shown */ public void step1() { move(); super.step1(); } public void step2() { turnLeft(); super.step2(); } } StepperBot ucla = new StepperBot(10, 10, North, 0); ucla.step1(); // where is the robot now? MoreStepperBot usc = new MoreStepperBot(10, 10, North, 0); usc.step1(); // where is the robot now? (11, 10) facing West (12, 10) facing South Since step1() was called from MoreStepperBot, it looks for step2() in MoreStepperBot first

10 “A” is-a Letter Letter is-a Symbol Semicolon is-a PunctuationMark is-a Symbol PunctuationMark is-a Symbol LetterRobot is-a BetterRobot BetterRobot is-a UrRobot Now, split into teams and arrange the objects in the proper inheritance hierarchy

11 Symbol LetterRobot Semicolon PunctuationMark “A” BetterBot UrRobot Letter