Question
Complete PowerPlant.java by implementing a Publisher-Subscriber mechanism. PowerPlant.java: package pubsub; class Reactor extends Publisher { private double temperature = 1000; private final double critical =
Complete PowerPlant.java by implementing a Publisher-Subscriber mechanism.
PowerPlant.java:
package pubsub;
class Reactor extends Publisher { private double temperature = 1000; private final double critical = 1500; public boolean tooHot() { return critical <= temperature; } public double getTemperature() { return temperature; } public void inc(double amt) { temperature += amt; } public void dec(double amt) { temperature -= amt; } }
class BeepingAlarm implements Subscriber { Reactor myReactor; public void update() { if (myReactor.tooHot()) { System.out.println('\u0007'); // beep } } }
class PrintingAlarm implements Subscriber { Reactor myReactor; public void update() { if (myReactor.tooHot()) { System.out.println("Warning: reactor too hot!"); } } } }
public class PowerPlant { public static void main(String[] args) { Reactor r = new Reactor(); BeepingAlarm alarm1 = new BeepingAlarm(r); PrintingAlarm alarm2 = new PrintingAlarm(r); r.inc(100); r.inc(100); r.inc(100); r.inc(100); r.inc(100); r.inc(100); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started