Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Control Structures.
Can you tell the letter each picture begins with?
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
01_07 Thermostat as example of homeostatic mechanism
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
An introduction to arrays
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Section 4.2: Functions that Test Conditions (continued)
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
Control Logic/Process Logic Summary and Worked out Example 2 Natalie Duchene Jeff Byrd Seung Youn Lyu.
Java Coding 3 David Davenport Computer Eng. Dept.,
Future Learning – Listen to the Kids David Davenport Computer Eng. Dept., Bilkent University Derya Davenport BUPS, Bilkent,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Robo David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to the engineering.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Would it be ok if you never had to go to work again? Lynda Ncube.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Computer Science – Truth Jam Table JamBreadSandwich Yes No YesNo YesNo TeaMilkDrink No Yes NoYes NoYes What am I getting at?
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
Doc.: IEEE /0773r1 Submission July 2007 Rick Alfvin, VeriLANSlide 1 Project: IEEE Working Group for Wireless Personal Area Networks (WPANs)
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Your Mission Objectives
From Mind to Brain Machine The Architecture of Cognition David Davenport Computer Eng. Dept., Bilkent University, Ankara – Turkey.
The Fine Points of Conditionals. When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
A: A: double “4” A: “34” 4.
System A collection of parts that perform a function.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Coding 6 David Davenport Computer Eng. Dept.,
How to Draw a Systems Diagram
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 2 David Davenport Computer Eng. Dept.,
Java Coding Extra David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept.,
A collection of parts that perform a function
null, true, and false are also reserved.
Java Coding 4 David Davenport Computer Eng. Dept.,
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Input-Output-Process Demo
Java Coding 8 David Davenport Computer Eng. Dept.,
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
General Guide to Thermostat Settings
Revision I This presentation gives you some hints for solution of selected problems.
Midterm Review October 23, 2006 ComS 207: Programming I (in Java)
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Fractions – Comparing – True or False Maze
Percentage – Increase – Calculator – True or False Maze
Sequences – Geometric – Shootout!
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Prime Numbers – True or False Maze
Primes, Factors & Multiples – Shootout!
Percentage – Decrease – Calculator – True or False Maze
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming & Interfaces

IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

Central Heating control… Would like to model a room with a thermostatically controlled heater Need thermostat & heater objects Thermostat must “tell” heater to switch off when room warm enough and on again when too cold

Thermostat class int lowerLimit, upperLimit boolean on // true if >= upperLimit, false if <= lowerLimit public Thermostat( int lower, int upper) public boolean isOn() public void update( int reading) if !on & reading >= upperLimit then set on and tell heater to switch off if on & reading <= lowerLimit then set off and tell heater to switch on

Heater class boolean state public Heater() public boolean getState() public setState( state)

Sending messages… Thermostat “tells” Heater to switch on/off In update method of Thermostat public void update( int reading) { if ( !on && reading >= upperLimit) {on = true; h.setState( false);} else if ( on && reading <= lowerLimit) {on = false; h.setState( true);} } Need to connect Heater to Thermostat Heater h; // new property added public void setHeater( Heater h) { this.h = h; } h is heater

A Central-heating system… Object Diagram roomTemp theater {Thermostat} {Heater} state lowerLimit upperLimit on h setState(…)

A Central-heating system… Thermostat t = new Thermostat(18, 20); Heater heater = new Heater(); t.setHeater( heater); int roomTemp = 0; heater.setState( true); for ( int time = 0; time < 100; time++) { if ( heater.getState() )// if heater is on roomTemp++;// temp increases else roomTemp--;// else decreases! t.update( roomTemp); System.out.println( roomTemp); }

Generalise… OK if only want Thermostats to switch Heaters on and off, BUT would also like to use them to control AirConditioners, and to ring AlarmBells, etc! HOW?

Generalise… {Thermostat} {Heater} setState( false) {AirConditioner} setState( true) {AlarmBell} ring() How can we have a variable to hold any of these things? And have them all do different things?

How… {Thermostat} handleAlarm class AlarmListener Define as abstract parent class {AlarmListener} (abstract) {Heater} setState {AirConditioner} setState {AlarmBell} ring is_a handleAlarm {AlarmListener} handleAlarm abstract void handleAlarm(…)

Thermostat class int lowerLimit, upperLimit boolean on public Thermostat( int lower, int upper) public int isOn() public void update( int reading) if !on & reading >= upperLimit then set on and alarmListener.handleAlarm( false); if on & reading <= lowerLimit then set off and alarmListener.handleAlarm( true); AlarmListener alarmListener; public void addAlarmListener( AlarmListener listener) Heater must be an AlarmListener

Heater class (extends AlarmListener) boolean state public Heater() public boolean getState() public setState( state) handleAlarm( boolean b) setState( b); AlarmListener {abstract} handleAlarm() Heater handleAlarm() is_a

Boiling water alarm… Would like to model a kettle which sounds an alarm bell when water boils Need thermostat & alarm bell objects Thermostat must “tell” alarm bell to ring when water boils Need AlarmBell to extend AlarmListener BUT… AlarmBell already in Bell class hierarchy!

The Bell Hierarchy Cannot “extend” more than one class! AlarmListener {abstract} handleAlarm() Heater handleAlarm() is_a Bell {abstract} ring() AlarmBell ring() DoorBell ring() is_a Must add handleAlarm

The Bell Hierarchy Bell {abstract} ring() AlarmBell ring() DoorBell ring() is_a {interface} AlarmListener handleAlarm() Heater handleAlarm() implements Solution - make AlarmListener an Interface Must add handleAlarm

Connecting Objects… What information is needed? {Thermostat} {AlarmListener} handleAlarm(-) {AlarmListener} {AlarmInfo} source reading nothing boolean Object Thermostat ” & reading AlarmInfo source