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,

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.
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.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
1 Inheritance in Java Behind the scenes: new Objects from old.
Problem Solving #1 ICS Outline Review of Key Topics Review of Key Topics Example Program Example Program –Problem 7.1 Problem Solving Tips Problem.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
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.
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.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
CPS Today’s topics Java Language Inheritance Upcoming Electric Circuits (not in text) Reading Great Ideas, Chapters 5.
Chapter 5 Conditionally Executing Instructions
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.
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 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.
CMSC 104, Version 8/061L17Top-DownDesign.ppt Top-Down Design Topics Top-Down Design Top-Down Design Examples The Function Concept Reading Sections 3.1.
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 CS161 Introduction to Computer Science Topic #9.
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
CS 4240: Rethinking Some OOP Ideas and Terms for OOA&D Readings: Chap. 8 in Shalloway and Trott (referred to as S&T in these slides) Wikipedia on information.
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 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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 Chapter 5 - IF CH5 – Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
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.
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.
For Monday Read Becker, chapter 4, sections 1 and 2.
Chapter 3 Extending the Robot Programming Language.
Karel – Primitive Instructions
Copyright © 2008 by Helene G. Kershner
Ch.3 Classes & Stepwise Refinement
UNIT 3 – LESSON 5 Creating Functions.
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.
Ch.3 Classes STEP 1 Define a new class of robot (see next slide)
Karel – Primitive Instructions
Polymorphism Simple but profound!.
Ch.3 Classes & Stepwise Refinement
Object References a.k.a. variables
CH5 – Conditional Statements
Ch.3 Classes & Stepwise Refinement
Ch.3 Classes & Stepwise Refinement
Algorithms, Part 3 of 3 Topics Top down-design Reading
Karel – Primitive Instructions
Presentation transcript:

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, bank accounts, 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!! )

2 Ch. 3 import kareltherobot.*; public class MileWalker extends ur_Robot { 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 subclass constructor INHERITANCE invokes superclass’ constructor

3 Ch. 3 Inserting MileWalker into the Inheritance Hierarchy ur_Robot 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 ur_Robot? the is-A relationship a MileWalker is-A ur_Robot

4 Ch. 3 STEP 2 write application(client) to use new class(server) (a.k.a. a driver) import kareltherobot.*; public class MileWalkerDriver 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 }

5 Ch. 3 Misc. Note (don’t sweat this – it’s not computer science) These 4 method invokations may be necessary and may be placed first within the main() of the driver –World.reset(); –World.readWorld(“c:\\first.kwld"); –World.setDelay(50); –World.setVisible(true); –Alternatively: you can place them in a static block (no need to understand what a “static block” is) within your driver class

6 Ch. 3 STEP 3 Put each class in its own file making sure that the file name matches the class name Exactly (convention: class names begin with a capital letter, method names begin with a lowercase letter – if an identifier combines several words, use the “interCap” technique. We will follow convention.) We’ll now demo the whole process in BlueJ

7 Ch. 3 Now You Try! we want a BetterTurnerRobot class –turnRight, turnAround, stepBackward

8 Ch. 3 Now, try something a bit more! 1.Design an HBot class on paper right now (yes, it should do the same thing as our non-class version in Ch.2) –In 10 minutes, we’ll pass the wireless keyboard & mouse and we’ll build/test the class together –In addition, where does it go in the Inheritance Hierarchy? 2.Why is putting all the same code into one method within a class (encapsulation) better than just leaving it in the driver – i.e., what do we gain? (let’s informally discuss before I give you the fancy cs language)

9 Ch. 3 Benefits of Encapsulation So, we just learned that encapsulation promotes Code Reuse. By putting the code into a method, we no longer need to write that code again. In addition, from the client’s point of view, she is no longer concerned with how to draw the H – she’s the beneficiary of a concept called Abstraction (can focus on WHAT, not HOW). In general, if you find yourself doing a cut-and-paste, then there is a better way to do things – i.e., use procedural abstraction and abstract out the common code, putting it into a method In that light, how should we now modify our current 1-method(if that’s how you wrote it) HBot?

10 Ch. 3 Improving HBot Yep, find the common code and create other methods. –drawLine() and turnRight() might be what you choose – you might choose others depending on how you see the problem being decomposed (stepwise refined) –Should they have public or private visibility? That depends on whether we believe a client would be calling drawLine() and turnRight() – let’s discuss public/private, then you guess/justify I’ll argue, No. I’ll argue like this: the name of the class is HBot – so I (the client) am trying to have some object draw H’s for me – how that object gets it done is of no concern of mine(abstraction) – so I don’t need (and shouldn’t be allowed) to see the other helper/auxiliary methods. Therefore, they should be private, helper-like methods for the class’ use only.

11 Ch. 3 Stepwise Refinement technique for writing modules which are concise, correct, easy to read/modify/understand –Would a general contractor just start building a house – or would she break up the task into foundation, frame, electrical, plumbing, etc.? Makes sense, doesn’t it. Explain why from the contractor’s view – use our cs terms we’ve been learning. write main task first, breaking up the BIG task into smaller tasks (using methods) – then take each method one at a time and also break it up --- continue until each method is compact and singular in focus (cohesion) Look back at what we just did – do you see this re-factoring?

12 Ch. 3 Practicing Stepwise Refinement Let’s write a class called DiamondPlanter together using stepwise refinement. It’s like Harvester except the field is diamond shaped. There are always 4 beepers on a diagonal. Assume the robot is facing North to begin, has 16 beepers, and is standing on the corner where the bottom of the diamond is to be. –What are we asking ourselves first? –Now, using some sort of pseudocode, write the top level method(call it, plantDiamond() ). While writing it, pretend any helper methods you’d like to use already exist (abstraction) and work already (automagically, if you will). After we pseudocode plantDiamond(), we’ll take each helper method in turn and repeat this stepwise-refinement process until we have cohesion.

13 Ch. 3 Debriefing DiamondPlanter So, we wrote DiamondPlanter. More than likely we wrote a turnRight() and maybe even a turnAround() to help us plant. 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 solution. Let’s discuss possible solutions before going on…

14 Ch. 3 Improving overall object design ur_Robot BetterTurnerBot HBotDiamondPlanter turnRight() turnAround() Discuss what our modifications would be to the HBot and DiamondPlanter classes – in terms of syntax - in terms of concepts we’ve been discussing

15 Ch. 3 Now YOU try! But be efficient! starts off facing East design a robot class that would be conducive to solving the following diagrammed situation (robot should climb and pick up all beepers – always 3/stair) also, different clients need to be able to climb different numbers of stairs: When designing, keep in mind everything we’ve been discussing.

16 Ch. 3 Why create a Class? 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 system to have a bot 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

17 Ch. 3 Why use Inheritance? You get stuff for free! You can be lazy! (cs term?) Use things without knowing/caring how they work! (cs term?) Why reinvent the wheel! (cs term?) Localize changes to one class/method (localization) StairClimberMileWalkerHarvesterDiamondPlanter BetterTurner ur_Robot