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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!