Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

/////////////////////GreenhouseControls//////////////////////////////////////////

public class GreenhouseControls extends Controller { private boolean light = false; private boolean water = false; private String thermostat = "Day"; private String eventsFile = "examples1.txt";

public class LightOn extends Event { public LightOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn on the light. light = true; } public String toString() { return "Light is on"; } } public class LightOff extends Event { public LightOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn off the light. light = false; } public String toString() { return "Light is off"; } } public class WaterOn extends Event { public WaterOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. water = true; } public String toString() { return "Greenhouse water is on"; } } public class WaterOff extends Event { public WaterOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. water = false; } public String toString() { return "Greenhouse water is off"; } } public class ThermostatNight extends Event { public ThermostatNight(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. thermostat = "Night"; } public String toString() { return "Thermostat on night setting"; } } public class ThermostatDay extends Event { public ThermostatDay(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. thermostat = "Day"; } public String toString() { return "Thermostat on day setting"; } } // An example of an action() that inserts a // new one of itself into the event list: public class Bell extends Event { public Bell(long delayTime) { super(delayTime); } public void action() { // nothing to do } public String toString() { return "Bing!"; } }

public class Restart extends Event { public Restart(long delayTime, String filename) { super(delayTime); eventsFile = filename; }

public void action() { addEvent(new ThermostatNight(0)); addEvent(new LightOn(2000)); addEvent(new WaterOff(8000)); addEvent(new ThermostatDay(10000)); addEvent(new Bell(9000)); addEvent(new WaterOn(6000)); addEvent(new LightOff(4000)); addEvent(new Terminate(12000)); }

public String toString() { return "Restarting system"; } }

public class Terminate extends Event { public Terminate(long delayTime) { super(delayTime); } public void action() { System.exit(0); } public String toString() { return "Terminating"; } }

public static void printUsage() { System.out.println("Correct format: "); System.out.println(" java GreenhouseControls -f , or"); System.out.println(" java GreenhouseControls -d dump.out"); }

//--------------------------------------------------------- public static void main(String[] args) { try { String option = args[0]; String filename = args[1];

if ( !(option.equals("-f")) && !(option.equals("-d")) ) { System.out.println("Invalid option"); printUsage(); }

GreenhouseControls gc = new GreenhouseControls();

if (option.equals("-f")) { gc.addEvent(gc.new Restart(0,filename)); }

gc.run(); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Invalid number of parameters"); printUsage(); } }

} ///:~

///////Controler.java//////////////////////////////////////////

public class Controller { // A class from java.util to hold Event objects: private List eventList = new ArrayList(); public void addEvent(Event c) { eventList.add(c); }

public void run() { while(eventList.size() > 0) // Make a copy so you're not modifying the list // while you're selecting the elements in it: for(Event e : new ArrayList(eventList)) if(e.ready()) { System.out.println(e); e.action(); eventList.remove(e); } } }

/////////////Event.java//////////////////////

package tme3;

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() { // Allows restarting eventTime = System.currentTimeMillis() + delayTime; } public boolean ready() { return System.currentTimeMillis() >= eventTime; } public abstract void action();

/////////////////////GreenhouseControls//////////////////////////////////////////

public class GreenhouseControls extends Controller { private boolean light = false; private boolean water = false; private String thermostat = "Day"; private String eventsFile = "examples1.txt";

public class LightOn extends Event { public LightOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn on the light. light = true; } public String toString() { return "Light is on"; } } public class LightOff extends Event { public LightOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here to // physically turn off the light. light = false; } public String toString() { return "Light is off"; } } public class WaterOn extends Event { public WaterOn(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. water = true; } public String toString() { return "Greenhouse water is on"; } } public class WaterOff extends Event { public WaterOff(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. water = false; } public String toString() { return "Greenhouse water is off"; } } public class ThermostatNight extends Event { public ThermostatNight(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. thermostat = "Night"; } public String toString() { return "Thermostat on night setting"; } } public class ThermostatDay extends Event { public ThermostatDay(long delayTime) { super(delayTime); } public void action() { // Put hardware control code here. thermostat = "Day"; } public String toString() { return "Thermostat on day setting"; } } // An example of an action() that inserts a // new one of itself into the event list: public class Bell extends Event { public Bell(long delayTime) { super(delayTime); } public void action() { // nothing to do } public String toString() { return "Bing!"; } }

public class Restart extends Event { public Restart(long delayTime, String filename) { super(delayTime); eventsFile = filename; }

public void action() { addEvent(new ThermostatNight(0)); addEvent(new LightOn(2000)); addEvent(new WaterOff(8000)); addEvent(new ThermostatDay(10000)); addEvent(new Bell(9000)); addEvent(new WaterOn(6000)); addEvent(new LightOff(4000)); addEvent(new Terminate(12000)); }

public String toString() { return "Restarting system"; } }

public class Terminate extends Event { public Terminate(long delayTime) { super(delayTime); } public void action() { System.exit(0); } public String toString() { return "Terminating"; } }

public static void printUsage() { System.out.println("Correct format: "); System.out.println(" java GreenhouseControls -f , or"); System.out.println(" java GreenhouseControls -d dump.out"); }

//--------------------------------------------------------- public static void main(String[] args) { try { String option = args[0]; String filename = args[1];

if ( !(option.equals("-f")) && !(option.equals("-d")) ) { System.out.println("Invalid option"); printUsage(); }

GreenhouseControls gc = new GreenhouseControls();

if (option.equals("-f")) { gc.addEvent(gc.new Restart(0,filename)); }

gc.run(); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Invalid number of parameters"); printUsage(); } }

} ///:~

///////Controler.java//////////////////////////////////////////

public class Controller { // A class from java.util to hold Event objects: private List eventList = new ArrayList(); public void addEvent(Event c) { eventList.add(c); }

public void run() { while(eventList.size() > 0) // Make a copy so you're not modifying the list // while you're selecting the elements in it: for(Event e : new ArrayList(eventList)) if(e.ready()) { System.out.println(e); e.action(); eventList.remove(e); } } }

/////////////Event.java//////////////////////

package tme3;

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() { // Allows restarting eventTime = System.currentTimeMillis() + delayTime; } public boolean ready() { return System.currentTimeMillis() >= eventTime; } public abstract void action();

//////////////DataFile.txt//////////////////////

Event=ThermostatNight,time=0 Event=LightOn,time=2000 Event=WaterOff,time=8000 Event=ThermostatDay,time=10000 Event=Bell,time=9000 Event=WaterOn,time=6000 Event=LightOff,time=4000 Event=Terminate,time=12000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions