Thermostat & AlarmListener

Download Report

Transcript Thermostat & AlarmListener

Java Coding OOP
Towards Event-driven programming & Interfaces
David Davenport
Computer Eng. Dept.,
Bilkent University
Ankara - Turkey.
email: [email protected]
IMPORTANT…

Students…
This presentation is designed to be used in class as
part of a guided discovery sequence. It is not selfexplanatory! 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.
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
Note: may want to use
thermostats for other tasks too!
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) {
h is heater
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;
}
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);
}
A Central-heating system…

Object Diagram
roomTemp
t
heater
{Thermostat}
{Heater}
lowerLimit
upperLimit
on
h
state
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}
How can we
have a
variable to
hold any of
these things?
{Heater}
setState( true)
{AirConditioner}
{AirConditioner}
And have
them all do
different
things?
{Heater}
setState( false)
{AlarmBell}
{AlarmBell}
ring()
How…
class AlarmListener
void handleAlarm()
Define as abstract parent
class or interface?
handleAlarm
extends
{AlarmListener}
{Thermostat}
handleAlarm
extends
{AlarmListener}
(abstract)
extends
setState
setState
ring
handleAlarm
handleAlarm
handleAlarm
{Heater}
{AirConditioner}
{AlarmBell}
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( this);
if on & reading <= lowerLimit then
set off and alarmListener.handleAlarm( this);



AlarmListener alarmListener;
Heater must be
an
 public void addAlarmListener(
AlarmListener listener)AlarmListener
Heater class





boolean state
AlarmListener
public Heater()
public boolean getState()
public setState( state)
handleAlarm(Object o)
setState( !((Thermostat) o).isOn() );
{abstract}
handleAlarm()
is_a
Heater
handleAlarm()
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
AlarmListener
{abstract}
handleAlarm()
Cannot
“extend”
more
than one
class!
is_a
Bell
{abstract}
ring()
is_a
is_a
is_a
Heater
handleAlarm()
AlarmBell
ring()
Must add
handleAlarm
DoorBell
ring()
The Bell Hierarchy

Solution - make AlarmListener an Interface
{interface}
Bell
AlarmListener
{abstract}
ring()
handleAlarm()
implements
is_a
is_a
implements
Heater
handleAlarm()
AlarmBell
ring()
Must add
handleAlarm
DoorBell
ring()
Java Interfaces

Design by interface gives flexibility

The Java API has a lot of interfaces
Iterator
 Comparable
 Serializable
 & a multitude for
GUI event-handling!

Java’s Iterator Interface



boolean hasNext()
Object next()
void remove()
To iterate (to go)
through a collection,
processing each
element once and once
only
// Scanner class implements Iterator
tokens = new Scanner( “To be or not to be”);
while ( tokens.hasNext() )
System.out.println( tokens.next() );
Java’s Iterator Interface



boolean hasNext()
Object next()
void remove()
// StringTokenizer
The Enumeration
Interface
Similar to, but older
than Iterator. Different
names & does not have
remove method.
Examples include
StringTokenizer & Vector
(which also has Iterator!)
tokens = new StringTokenizer(
“To be or not to be”);
while ( tokens.hasMoreElements() )
System.out.println( tokens.nextElement() );
Java’s Iterator Interface



boolean hasNext()
Object next()
void remove()
Surprisingly, ArrayList
does not implement
Iterator.
// given an ArrayList, list
Iterator x = list.iterator();
while ( x.hasNext() )
System.out.println( x.next() );
It implements
Iterable!
Iterator iterator()
Java’s Comparable Interface

int compareTo( Object o)
compare this object with o
and return negative, zero, positive
to indicate <, =, > respectively!
// Assuming x implements Comparable
//
e.g. String
if ( x.compareTo( y) < 0 )
exchange( x, y);
Java’s Serialization Interface

Allows Java objects to be converted
to/from a stream of bytes!

Rather special since you do not need to
write any methods, merely




Make class implement Serializable
Have a default constructor
Ensure all properties are Serializable
Check the Java API documentation
for details and example code.