Question: Create the following interface interface Fixable { // turns Power on, fix window and zeros out error codes void fix (); // logs to a
Create the following interface
- interface Fixable {
// turns Power on, fix window and zeros out error codes
void fix ();
// logs to a text file in the current directory called fix.log
// prints to the console, and identify time and nature of
// the fix
void log();
}
2. Create inner classes PowerOn and FixWindow that implement Fixable.
3. Add a method to GreenhouseControls
- int getError();
which returns the error code saved.
4. Add a method within GreenhouseControls
- Fixable getFixable(int errorcode);
which returns the appropriate Fixable object to correct the error and reset the error code to zero.
5. Create a class Restore to restore the system after a shutdown. When this command line is called:
- java GreenhouseControls -d dump.out
a Restore object should be created and the file name dump.out is passed to the Restore object. It then retrieves the GreenhouseControls object saved by any emergency shutdown. It has the state of the saved system printed to the console. It sets any variable that was changed by the Event that initiated the shutdown back to its normal state (e.g., poweron=true) using the appropriate Fixable. It then starts the system running at the event following the event that caused the shutdown. A proper error message should be displayed on the console if any exception or error is caught by the Restore object.
/////////////////////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
//--------------------------------------------------------- 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
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
/////////////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
//--------------------------------------------------------- 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
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
/////////////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
Get step-by-step solutions from verified subject matter experts
