Your task is to complete the implementation of four classes: RadiationSensor whi
ID: 3736633 • Letter: Y
Question
Your task is to complete the implementation of four classes:
RadiationSensor which extends Observable. Because we do not have a real reactor to monitor, we will simulate radiation readings using a random number generator. readRadiation() should set a private radiation variable to a random double between 0 and 10. Hint: to assist you with the remainder of readRadiation(), inspect the JDK class Observable.
RadiationMonitor which implements the Observer interface. Note that now() is already complete, but you will need to implement the rest of the class except for the abstract methods.
Explanation / Answer
package patt.ReactorMonitoring; import java.util.Observable; public class ResearchCentre extends RadiationMonitor { /** * Constructs a ResearchCentre object, which observes reactor radiation readings * and constantly prints a report with the current moving average of the * recorded observations. * * @param location. * An arbitrary location. */ public ResearchCentre(String location) { super(null); } /** * Updates the monitor with a new observation and prints a report. */ public void update(Observable subject, Object o) { } /** * Generates a report of the current moving average, updated by a new * observation. The moving average is calculated by summing all observations * made so far, and dividing by the quantity of observations so far. */ public String generateReport() { return null; } } package patt.ReactorMonitoring; public class ReactorMonitoring { public static void main(String[] args) { // Install a sensor at Reactor A RadiationSensor sensor = new RadiationSensor("Reactor A", 10); // Create sensor observers RadiationMonitor control = new ControlRoom("Reactor A Control Room", 8.0); RadiationMonitor science = new ResearchCentre("Centre for Nuclear Research"); // Attach observers to sensor sensor.addObserver(control); sensor.addObserver(science); // Simulate a scenario try { while (true) { sensor.readRadiation(); Thread.sleep(4000); } } catch (InterruptedException e) { e.printStackTrace(); } } } package patt.ReactorMonitoring; import java.util.Observable; public class RadiationSensor extends Observable { /** * Constructs a RadiationSensor object * * @param location. * An arbitrary location. * @param seed. * A seed for the random number generator used to simulate radiation * readings. */ public RadiationSensor(String location, int seed) { } /** * Gets the location * * @return location */ public String getLocation() { return null; } /** * Gets the radiation * * @return radiation */ public double getRadiation() { return 0; } /** * Updates radiation, changes the state to true, and notifies all observers of * the change. */ public void readRadiation() { } } package patt.ReactorMonitoring; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Observable; import java.util.Observer; public abstract class RadiationMonitor implements Observer { /** * Constructs a RadiationMonitor object. * * @param location. * An arbitrary location. */ public RadiationMonitor(String location) { } /** * Gets the location * * @return location */ public String getLocation() { return null; } /** * Updates the monitor with a new observation */ public abstract void update(Observable subject, Object o); /** * Generates a report based on the observation. * * @param observation * @return a report */ public abstract String generateReport(); /** * Gets the current time. * * @return The current time in yyyy-MM-dd HH:mm:ss format */ public String now() { String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; Calendar calendar = Calendar.getInstance(); SimpleDateFormat date = new SimpleDateFormat(DATE_FORMAT_NOW); return date.format(calendar.getTime()); } } package patt.ReactorMonitoring; import java.util.Observable; public class ControlRoom extends RadiationMonitor { /** * Constructs a ControlRoom object, which observes reactor radiation readings * and prints reports if the radiation exceeds a threshold. * * @param location. * An arbitrary location. * @param warningThreshold. * The radiation threshold for when reports should be printed. */ public ControlRoom(String location, double warningThreshold) { super(null); } /** * Updates the monitor with a new observation and prints a report if and only if * the observation is equal to or greater than the warning threshold. */ public void update(Observable subject, Object o) { } /** * Generates a report based on the current observation. */ @Override public String generateReport() { return null; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.