Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &"— Presentation transcript:

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

2 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

3 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

4 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

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

6 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

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

8 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); }

9 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?

10 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?

11 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(…)

12 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

13 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

14 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!

15 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

16 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

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


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

Similar presentations


Ads by Google