1 / 16

Java Coding OOP

Java Coding OOP. Towards Event-driven programming & Interfaces. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr. IMPORTANT…. Students…

cuyler
Download Presentation

Java Coding OOP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Coding OOP Towards Event-driven programming & Interfaces David Davenport Computer Eng. Dept., Bilkent UniversityAnkara - Turkey. email: david@bilkent.edu.tr

  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 email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you,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 enoughand 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. lowerLimit state upperLimit on h A Central-heating system… • Object Diagram roomTemp t heater {Thermostat} {Heater} 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. {Thermostat} {Heater} {Heater} setState( false) {AirConditioner} {AirConditioner} setState( true) {AlarmBell} {AlarmBell} ring() Generalise… How can we have a variable to hold any of these things? And have them all do different things?

  11. {AlarmListener}(abstract) handleAlarm is_a is_a is_a setState setState ring handleAlarm {Heater} {AirConditioner} {AlarmBell} {Thermostat} How… class AlarmListener abstract void handleAlarm(…) Define as abstract parent class {AlarmListener} handleAlarm handleAlarm 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); Heater must be an AlarmListener • AlarmListener alarmListener; • public void addAlarmListener( AlarmListener listener)

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

  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. AlarmListener {abstract} handleAlarm() Bell {abstract} ring() is_a is_a is_a is_a AlarmBellring() DoorBell ring() HeaterhandleAlarm() The Bell Hierarchy Cannot “extend” more than one class! Must add handleAlarm

  16. {interface}AlarmListener handleAlarm() Bell {abstract} ring() implements is_a is_a implements AlarmBellring() DoorBell ring() HeaterhandleAlarm() The Bell Hierarchy • Solution - make AlarmListener an Interface Must add handleAlarm

More Related