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.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Nested If Statements While Loops
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 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
OBJECT ORIENTED PROGRAMMING M Taimoor Khan
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
1 Inheritance in Java Behind the scenes: new Objects from old.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
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.
Computer Science I Inheritance Professor Evan Korth New York University.
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.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Chapter 5 Conditionally Executing Instructions
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
Inheritance Abstract Classes Check out Inheritance from SVN.
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.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Karel the Robot A Gentle Introduction to the Art of Programming.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
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. 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.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
Object Oriented Programming: Inheritance Chapter 9.
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.
Coming up: Inheritance
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
1 Chapter 5 - IF CH5 – Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
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,
Java Software Solutions Lewis and Loftus Chapter 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. 1 Inheritance -- Introduction.
Module 9. Dealing with Generalization Course: Refactoring.
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.
Alice and Java Unit 7 1. Day 1  Objective: Gain an introduction to Java and Eclipse  Essential skill: DM-1: Use technology to advance critical thinking.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
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.
Boğaziçi Ünv Koç Ünv Darüşşafaka Lisesi
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
Ch.3 Classes & Stepwise Refinement
Copyright © 2008 by Helene G. Kershner
Karel J Robot.
Ch.3 Classes STEP 1 Define a new class of robot (see next slide)
Road Map Inheritance Class hierarchy Overriding methods Constructors
Karel J Robot OOP approach to learning computer science
Object Oriented Analysis and Design
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
CH5 – Conditional Statements
Nested If Statements While Loops
Ch.3 Classes & Stepwise Refinement
Inheritance and Polymorphism
Ch.3 Classes & Stepwise Refinement
Presentation transcript:

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

2 Karel J Robot-Lesson 3 NTSD-Mr. Moon Debriefing Programs HBot, Harvester, DiamondPlanter We wrote HBot, Harvester, and DiamondPlanter. We wrote a turnRight() and maybe even a turnAround() or stepBackwards() to help us in each of the 3 robot classes. Anyone want to make any comments about that? –I don’t know about you, but I found it ANNOYING to write the same thing again! Let’s look at the Inheritance Hierarchy and see if we can come up with a better solution. –Hint: We extended UrRobot with all of our new Robot classes, what could we have extended that would have been better? Did I hear you say BetterTurnerRobot

3 Karel J Robot-Lesson 3 NTSD-Mr. Moon Improved Object Design UrRobot BetterTurnerRobot HBot DiamondPlanter turnRight() turnAround() 1.These classes currently inherit from who? UrRobot 2.What class should they really inherit ? BetterTurnerRobot 3.What do we gain by this change? Code reuse – get turnRight, turnAround, stepBackwards for free 4.What 2 syntax changes would we need to make to the 3 robot class? extends BetterTurnerRobot instead of UrRobot Remove turnRight, turnAround methods as they will now be inherited (get for free) stepBackwards() Harvester Recall, we wrote turnRight for each of these classes For future programs, lets be sure we take advantage of BetterTurnerRobot Discuss changes to HBot, Harvester and DiamondPlanter Robot classes.

4 Karel J Robot-Lesson 3 NTSD-Mr. Moon Why Create a new Class (new type of Robot)? Why reinvent the wheel? You’re allowed to be lazy in my class – but you have to be smart to be lazy! Code Reuse Abstraction – 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.

5 Karel J Robot-Lesson 3 NTSD-Mr. Moon Why use Inheritance? You get stuff for free! You can be lazy! (what OO term is this?) –Inheritance (you get methods for free) Use things without knowing/caring how they work! (OO term ?) –Abstraction (using methods that come for free and you know what they do, but don't care or think about how it is done) Why reinvent the wheel! (OO term?) –Code Reuse Localize changes to one class/method (localization) –Ex: We could add more commands or change a command in BetterTurnerRobot. We only have to do the changes in this class, not, all of the classes. All other classes will get new change free. StairClimberHBotHarvesterDiamondPlanter BetterTurnerRobot UrRobot Our Inheritance Hierarchy

6 Karel J Robot-Lesson 3 NTSD-Mr. Moon Sample Inheritance Questions How many times does each Bot below move? MysteryBot1 extends UrRobot { /* constructor not shown */ public void step1() { move(); } public void move() { super.move(); super.move(); } } calls UrRobot's move MysteryBot2 extends MysteryBot1 { /* constructor not shown */ public void step1() { move(); } public void move() { super.move(); super.move(); } } MysteryBot1 john = new MysteryBot1(10, 10, North, 0); john.step1(); MysterBot2 sue = new MysterBot2(10, 10, North, 0); sue.step1(); John did 2 moves (going North) he is now at (12,10) Where is the bot (john) after this code runs? Where are the bots (john) and (sue) after this code runs? John is still at (12,10). Sue did 4 moves (going North) she is now at (14,10) Notice that both robots did step1 (but results were different due to inheritance)

7 Karel J Robot-Lesson 3 NTSD-Mr. Moon Another Inheritance Question Give the state (Location and Direction) of each Bot below. ABetterBot extends UrRobot { public void step1() { move(); } public void step2() { turnLeft(); } } AnEvenBetterBot extends ABetterBot { public void step1() { move(); super.step1(); step2(); } public void step2() { turnLeft(); super.step2(); } } AnEvenBetterBot cv = new AnEvenBetterBot(10, 10, North, 0); cv.step1(); What location is bot (elkland) at and where is it facing after this code runs? ABetterBot elkland = new ABetterBot(10, 10, North, 0); elkland.step1(); What location is bot (cv) at and where is it facing after this code runs? elkland moved north 1 block and is now at (11,10) facing north cv moved 2 blocks north, turnsLeft, turnsLeft and ends up at (12,10) facing south

8 Karel J Robot-Lesson 3 NTSD-Mr. Moon A Final Inheritance Question Give the state (location and Direction) of each Bot below? TestBot extends UrRobot { public void step1() { move(); step2(); } public void step2() { turnLeft(); } } HarderTestBot extends TestBot { public void step1() { move(); super.step1(); step2(); } public void step2() { turnLeft(); super.step2(); } TestBot frankenstein = new TestBot(5, 5, North, 0); frankenstein.step1(); What location is bot (frankenstein) at and where is it facing after this code runs? What location is bot (cartman) at and where is it facing after this code runs? HarderTestBot cartman = new HarderTestBot(5, 5, North, 0); cartman.step1(); cartman did move, move, turnLeft, turnLeft, turnLeft. He is now at (7,5) facing east. frankenstein did move, turnLeft. He is now at (6,5) facing west.

9 Karel J Robot-Lesson 3 NTSD-Mr. Moon Vocabulary Review Quiz Choices (inheritance, encapsulation, abstraction, stepwise refinement, precondition, override, superclass, subclass, private, public, code reuse, cohesion, super, constructor) I am the more "general/abstract" class –superclass Hiding code details and providing easy to use interface. –encapsulation I am a method and was marked this, I can be seen by all classes (ie: a client) –public Extracting essential details about an item and ignoring unimportant details –abstraction I am a subclass and want to call a method from my parent, so I must use this? –super This is used to break a BIG task into smaller tasks –stepwise refinement I am a benefit of using inheritance (ie: you can get turnRight for free) –code reuse I need to be true before a method can be executed –precondition I am used when a new robot is built so that I can properly configure it as requested –constructor A mechanism for enhancing an existing class –inheritance I am a method and was marked this, I can not be seen by a client class –private I am method that is singular in focus (I only do 1 thing). –cohesion I am the more "specific" type of class –subclass I am the basic move method and was inherited by a subclass, but was changed to move a mile now. –override