1
Reply

Delegates and Events

Shilpesh Sinha

Shilpesh Sinha

Aug 20 2015 1:13 AM
343
A Hotel room has a high-end water heater. When the water temperature exceeds a particular degrees,Water heater speaker announces the water temperature and the LCD displays the appropriate message. The user should enter the degree of the temperature where it starts to show the message.

Write a program to simulate the above mentioned process by defining a class called WaterHeater with attributes like waterTemperature, and methods like
BoilWater() – to change the temperature of water
MakeAlert() – to announce the current temperature of water, when it exceeds 95 degrees
ShowMsg() – to display the message that the “Temperature exceeds 95 degrees”

Water heater’s function is only to heat the water. It can announce or display the messages with the help of alarms and monitors, which can be from a different vendor or assembly.
In this case, the sequence of events should be like this:

1. Alarm and monitor are more interested in water heater’s temperature.
2. Water heater maintains a reference to the alarm and display.
3. The action of water heater: When the temperature exceeds 95 degrees it automatically calls MakeAlert () method and displays the ShowMsg () method.

Implement this Observer design pattern using delegates and events.


Sample Input andoutput Statements:
Enter the temperature:
96
Alarm: Beep beep beep, water is 96 degrees.:
Display: Water quickly boiled, the current temperature: 96 degrees.
Alarm: Beep beep beep, water is 97 degrees.:
Display: Water quickly boiled, the current temperature: 97 degrees.
Alarm: Beep beep beep, water is 98 degrees.:
Display: Water quickly boiled, the current temperature: 98 degrees.
Alarm: Beep beep beep, water is 99 degrees.:
Display: Water quickly boiled, the current temperature: 99 degrees.
Alarm: Beep beep beep, water is 100 degrees.:
Display: Water quickly boiled, the current temperature: 100 degrees.

Answers (1)