Polymorphism and Observers

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
Copyright, Joseph Bergin
Karel J Robot Chapter 6.
Chapter 3 Extending the Robot Programming Language.
1 Inheritance in Java Behind the scenes: new Objects from old.
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L13 (Chapter 21) Generics.
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.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
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.
Using Jeroo To Teach Object-Oriented Concepts By Christian Digout.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
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.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming Progamz pls. Importance VERY IMPORTANT.
Karel J. Robot A Gentle Introduction to the Art of Object Oriented Programming.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 3.
Object-Oriented Programming (Java), Unit 18 Kirk Scott 1.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
Karel the Robot A Gentle Introduction to the Art of Object- Oriented Programming in Java.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com Day 4.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Classes - Intermediate
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
Object Oriented Programming in Java Habib Rostami Lecture 10.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
1 Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com.
CSII Final Review. How many bits are in a byte? 8.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
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)
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Chapter 3 Extending the Robot Programming Language.
Written by: Dr. JJ Shepherd
Modern Programming Tools And Techniques-I
Karel – Primitive Instructions
Interface.
Karel J Robot.
Karel – Primitive Instructions
Karel J Robot.
Introduction to Object-oriented Program Design
Something about Java Introduction to Problem Solving and Programming 1.
Interface.
Interfaces and Constructors
Classes & Objects: Examples
What is Singleton Category: Creational pattern
A Gentle Introduction to the Art of Object Oriented Programming
Unit 1 Lab16.
Example with Static Variable
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
Java Programming with Multiple Classes
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Classes.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Karel – Primitive Instructions
CSG2H3 Object Oriented Programming
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Polymorphism and Observers Chapter 4.9 Polymorphism and Observers Karel J. Robot: A Gentle Introduction to the Art of Object-Oriented Programming in Java by Joseph Bergin et al. By E.F. Teevan

Observers - 4.9 An Observer is an object which is controlled by an observable object An interface is needed for the two to communicate: public interface RobotListeners { public void action(); } An observer example: public class WalkListener extends ur_Robot implements RobotListener { public WalkListener(int street, int avenue, Directions.Direction dir, int beepers) { super(street, avenue, dir, beepers); } public void action() { move(); } } Uses the RobotListener interface WalkListeners move one block when action method is called. WalkListeners are Observers

Observers II - 4.9 The observable object class: public class ObservablePicker extends ur_Robot { private RobotListener myListener; public ObservablePicker(int street, int avenue, Directions.Direction direction, int beepers) { super(street, avenue, direction, beepers); } public void register(RobotListener listener) { myListener = listener; } public void pickBeeper() { super.pickBeeper(); myListener.action(); } } instance variable constructor New method for ObservablePickers Instantiates the instance variable myListener New method for ObservablePicker Calls action method of RobotListener class for myListener

Observers III - 4.9 The main method: public class ObserverTest implements Directions { public static void main(String args[]) { WalkListener2 tom = new WalkListener2(1, 1, North, 0); ObservablePicker2 jerry = new ObservablePicker2(2, 3, East, 0); jerry.register(tom); jerry.pickBeeper(); } } CONTROLLED tom is an Observer jerry is observable CONTROLLER See fourNine in the Bowling workspace