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

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.
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Written by: Dr. JJ Shepherd
Department of computer science N. Harika. Inheritance Inheritance is a fundamental Object Oriented concept A class can be defined as a "subclass" of another.
1 Inheritance in Java Behind the scenes: new Objects from old.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Enhancing classes Visibility modifiers and encapsulation revisited
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
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.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
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 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Introducing Objects. Structure  Objects have two parts: Instance Variables (attributes, adjectives) Instance Variables (attributes, adjectives) private.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.
1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
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.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Computer Science I Classes and objects Classwork/Homework: Examine and modify my examples. Make your own.
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.
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.
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Written by: Dr. JJ Shepherd
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,
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
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.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Review for Test2. Scope 8 problems, 60 points. 1 Bonus problem (5 points) Coverage: – Test 1 coverage – Exception Handling, Switch Statement – Array of.
Karel – Primitive Instructions
Objects as a programming concept
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Polymorphism and Observers
Karel – Primitive Instructions
PHP Classes and Objects
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 3 Test: Friday.
Object References a.k.a. variables
Which best describes the relationship between classes and objects?
Review of Previous Lesson
Karel – Primitive Instructions
Presentation transcript:

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

2 polymorphism is ubiquitous (everywhere) in OOP there are many uses and examples of it let’s now build toward another example of polymorphism –but first, as last time, we need some setup…

3 one object controlling others (overseeing the movement of others) we now want a MoveChoreographer class, which, when constructed, is passed 3 friends (robots) the MoveChoreographer has one method called, moveFriends() which, when invoked, “moves” each friend once this Choreographer model of problem solving, by the way, can been seen in the “general contractor” analogy we used in the ppt from Ch. 3 - the general contractor doesn’t really do the work, she just delegates it to other objects

4 public class MoveChoreographer extends UrRobot { private UrRobot friendA; private UrRobot friendB; private UrRobot friendC; // constructor on next slide // other methods } instance variables objects not only do things (behavior), they can also remember things (state) using instance variables

5 public MoveChoreographer (int st, int av, Direction dir, int numBeepers, UrRobot botA, UrRobot botB, UrRobot botC ) { super (st, av, dir, numBeepers); // must come first in method friendA = botA; friendB = botB; friendC = botC; } instance variables being assigned

6 public void moveFriends() { friendA.move(); friendB.move(); friendC.move(); }

7 public class MoveChoreographer extends UrRobot { private UrRobot friendA; private UrRobot friendB; private UrRobot friendC; public MoveChoreographer (int st, int av, Direction dir, int numBeepers, UrRobot botA, UrRobot botB, UrRobot botC ) { super (st, av, dir, numBeepers); // must come first in method friendA = botA; friendB = botB; friendC = botC; } public void moveFriends() { friendA.move(); friendB.move(); friendC.move(); }

8 Can you now give some sample client code that uses a MoveChoreographer object? Pass a MileWalker, DropBeeperAnd Walker and a BackardWalker to the MoveChoreographer object (do so now for 5 minutes…) an example: UrRobot bot1 = new MileWalker(2, 4, North, 0) ; UrRobot bot2 = new DropBeeperAndWalker(2, 5, North, infinity); UrRobot bot3 = new BackwardWalker(2, 6, North, 0); MoveChoreographer chor; chor = new MoveChoreographer(1, 1, North, 0, bot1, bot2, bot3); chor.moveFriends(); draw a picture and show the before and after

9 The statement from the previous slide, chor = new MoveChoreographer(1, 1, North, 0, bot1, bot2, bot3); is kind of neat. When someone constructs a MoveChoreographer, he can pass any 3 robots in any order as long as each one is-A UrRobot (it extends from a UrRobot). The MoveChoreographer only wants to be guaranteed that it can perform a move() on any object passed to it - since there is a move() in UrRobot, it chose to make its parameters of type UrRobot, guaranteeing (to itself and the compiler) that it will be able to call move() at run-time. The term that describes which particular move() will be called at run-time is ____________. Polymorphism

10 Initial SituationEnd Situation Create a Gardener robot that will use GardenerHelper robots to help plant a garden. The Gardener class will have a constructor method similar to the MoveChoreographer robot that we created in class. Gardener’s specialty is to supervise 3 other GardenerHelper robots to plant beepers. You should program the GardenerHelper class to plant beepers around the plus-shaped wall. The GardenerHelper is a BetterRobot and the Gardener robot is a GardenerHelper. The Gardener robot should be programmed to use itself and the 3 other robots that are passed to it to plant each corner. The client program should pass the helper robots to the Gardener robot in the correct street/avenue that they should start on and facing in the correct direction. gardenerWorld.txt