Question
In java code, original answer only please. 1. Create a WindowMalfunction and PowerOut Events to simulate problems that may occur in a GreenhouseControls. The event
In java code, original answer only please.
1. Create a WindowMalfunction and PowerOut Events to simulate problems that may occur in a GreenhouseControls. The event should set the following boolean variables as appropriate in GreenhouseControls:
- windowok = false;
poweron = false;
After setting the variables, WindowMalfunction or PowerOut should throw an exception specifying the faulty condition. Create a ControllerException class that extends Exception for this purpose.
2. If an exception is thrown from WindowMalfunction or PowerOut, the Controller catches the exception, then initiates an emergency shutdown with an appropriate message. Add a method to Controller called shutdown, and override this method in GreenhouseControls to accomplish the shutdown.
3. Add an instance variable in GreenhouseControls called errorcode. It indicates the nature of the problem with an error code in an int variable errorcode (1 for WindowMalfunction and 2 for PowerOut), logs the time and the reason for the shutdown in a text file in the current directory called error.log and prints it to the console. It then serializes and saves the entire GreenhouseControls object in a file dump.out in the current directory before exiting.
------------------------------------------ Current code -----------------------------------------
import java.util.List; import java.util.ArrayList; public class Controller { private final ListeventList = new ArrayList<>(); public void addEvent(Event c) { eventList.add(c); } public void run() { while (eventList.size() > 0) { for (Event e : new ArrayList<>(eventList)) { if (e.ready()) { System.out.println(e); e.action(); eventList.remove(e); } } } } }
import java.io.*; public abstract class Event { private long eventTime; protected final long delayTime; public Event (long delayTime) { this.delayTime = delayTime; start(); } public void start() { eventTime = System.nanoTime() + delayTime; } public boolean ready() { return System.nanoTime() >= eventTime; } public abstract void action(); }
import java.io.*; import java.util.Calendar; public class GreenHouseControls extends Controller { public class LightOn extends Event { public LightOn(long delayTime) { super (delayTime); } public void action() { light = true; } //to turn the light on public String toString() { return "Light is on."; } } public class LightOff extends Event { public LightOff(long delayTime) { super (delayTime); } public void action() { light = false; } //to turn light off public String toString() { return "Light is off."; } } public class WaterOn extends Event { public WaterOn (long delayTime) { super(delayTime); } public void action() { water = true; } //water on public String toString() { return "Greenhouse water is on."; } } public class WaterOff extends Event { public WaterOff(long delayTime) { super(delayTime); } public void action() { water = false; } //water off public String toString() { return "Greenhouse water is off."; } } public class ThermostatOn extends Event { public ThermostatOn(long delayTime) { super(delayTime); } public void action() { thermostat = "Night."; } //set thermostat to night public String toString() { return "Thermostat on Night Setting."; } } public class ThermostatOff extends Event { public ThermostatOff(long delayTime) { super(delayTime); } public void action() { thermostat = "Day"; } //set thermostat to day public String toString() { return "Thermostat on Day Setting."; } } public class FansOn extends Event { public FansOn(long delayTime) { super(delayTime); } public void action() { Fans = true; } //fans on public String toString() { return "Fans are on."; } } public class FansOff extends Event { public FansOff(long delayTime) { super(delayTime); } public void action() { Fans = false; } // fans off public String toString() { return "Fans are off."; } } public class Bell extends Event { public Bell(long delayTime) { super(delayTime); } public void action() { addEvent(new Bell(delayTime)); } public String toString() { return "Bing!"; } } public class Restart extends Event { private Event[] eventList; public Restart (long delayTime, Event[] eventList) { super(delayTime); this.eventList = eventList; for (Event e : eventList) { addEvent(e); } } public void action() { for (Event e : eventList) { e.start(); addEvent(e); } start(); addEvent(this); } public String toString() { return "Restarting System."; } } public static class Terminate extends Event { public Terminate(long delayTime) { super (delayTime); } public void action() { System.exit(0); } public String toString() { return "Terminating."; } } boolean Fans = false; private String thermostat = "Day"; private boolean water = false; private boolean light = false; }
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