Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Design Model-Assignment

  1. #1
    Junior Member JavaStudent100's Avatar
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Design Model-Assignment

    Hi guys,

    I have this assignment and I am puzzled. Please help. Thanks.


    Step 2: In this part, we add implementation to existing design

    1. Add a boolean instance variable to GreenhouseControls called “fans”. Create two Event classes called FansOn and FansOff. The action() of these two classes should modify fan to true or false respectively.

    2. Modify Bell Event so that it will be able to run an arbitrary number of times separated by 2000 msec each. To facilitate this requirement, you should generate the number of Bell Events specified in the rings parameter in the examples file. Please pay special attention to the possibility that other events might be run in between the various Bell events.

    3. Modify Restart.action() to start the the system by reading events from a text file. Use Scanner and an appropriate regular expression.

    4. Try running GreenhouseControls by:

    java GreenhouseControls -f examples1.txtu
    and

    java GreenhouseControls -f examples2.txt

    5. The -f argument must be present. It must be either “-f” or “-d”. Please see Part 4. The event information must be in the format specified in examples1.txt and examples2.txt.


    Code with my modifications:

     
    import java.io.*;
    import java.util.Calendar;
    import tme3.*;
     
    public class GreenhouseControls extends Controller {
      private boolean fans=false;
      private boolean light = false;
      private boolean water = false;
      private String thermostat = "Day";
      private String eventsFile = "examples1.txt";
     
     
      public class FansOn extends Event {
    	    public FanOn(long delayTime) { super(delayTime); }
    	    public void action() {
    	      // Put hardware control code here to
    	      // physically turn on the fan.
    	      fans = true;
    	    }
    	    public String toString() { return "Fan is on"; }
      }
     
      public class FansOff extends Event {
    	    public FansOff(long delayTime) { super(delayTime); }
    	    public void action() {
    	      // Put hardware control code here to
    	      // physically turn off the fan.
    	      fans = false;
    	    }
    	    public String toString() { return "Fan is off"; }
    }
     
      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
        	addEvent(new Bell(2000));
        }
        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 <filename>, 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();
    	}
        }
     
    } ///:~


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Design Model-Assignment

    Welcome to the forum.

    What is it exactly you need help with?
    If you were given some "starter code" to work with please post the unmodified version of it so we can all see the starting point, and then post your modified version so we can see what you have added/changed.

  3. #3
    Junior Member JavaStudent100's Avatar
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Design Model-Assignment

    Quote Originally Posted by jps View Post
    Welcome to the forum.

    What is it exactly you need help with?
    If you were given some "starter code" to work with please post the unmodified version of it so we can all see the starting point, and then post your modified version so we can see what you have added/changed.
    Hi jps,

    Thanks for your prompt feedback. I am pretty new to Java programming. This is an intermediate level and I am totally lost. I need guidance around how to proceed. I have been testing different codes, but I must say I am totally confused. Here is the original code:

     
    //: innerclasses/GreenhouseControls.java
    // This produces a specific application of the
    // control system, all in a single class. Inner
    // classes allow you to encapsulate different
    // functionality for each type of event.
    // From 'Thinking in Java, 4th ed.' (c) Bruce Eckel 2005
    // www.BruceEckel.com. See copyright notice in CopyRight.txt.
     
     
    import java.io.*;
    import java.util.Calendar;
    import tme3.*;
     
    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 <filename>, 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();
    	}
        }
     
    } ///:~

    Here is what I have done:

    import java.io.*;
    import java.util.Calendar;
    import java.util.Scanner;
     
    import tme3.*;
     
    public class GreenhouseControls extends Controller {
      private boolean fans=false;
      private boolean light = false;
      private boolean water = false;
      private String thermostat = "Day";
      private String eventsFile = "examples1.txt";
     
     
      public class FansOn extends Event {
    	    public FansOn(long delayTime) { super(delayTime); }
    	    public void action() {
    	      // Put hardware control code here to
    	      // physically turn on the fan.
    	      fans = true;
    	    }
    	    public String toString() { return "Fan is on"; }
      }
     
      public class FansOff extends Event {
    	    public FansOff(long delayTime) { super(delayTime); }
    	    public void action() {
    	      // Put hardware control code here to
    	      // physically turn off the fan.
    	      fans = false;
    	    }
    	    public String toString() { return "Fan is off"; }
    }
     
      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
        	addEvent(new Bell(2000));
        }
        public String toString() { return "Bing!"; }
      }
     
      public class Restart extends Event {
        public Restart(long delayTime, String filename) {
          super(delayTime);
          eventsFile = filename;
        }
     
        public void action(){
        File text= new File("examples1.txt");
        Scanner scan= new Scanner(text);
        int lineNumber=1;
        while(scan.hasNextLine()){
        	String line= scan.nextLine();
        	System.out.println(lineNumber+line);
     
     
        }
     
     
     
        }
     
        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 <filename>, 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();
    	}
        }
     
    } ///:~

    Here is the Event class:

     
    //: innerclasses/controller/Event.java
    // The common methods for any control event.
    // From 'Thinking in Java, 4th ed.' (c) Bruce Eckel 2005
    // www.BruceEckel.com. See copyright notice in CopyRight.txt.
     
    /***********************************************************************
     * Adapated for COMP308 Java for Programmer, 
     *		SCIS, Athabasca University
     *
     * Assignment: TME3
     * @author: Steve Leung
     * @date  : Oct. 21, 2006
     *
     * Description: Event abstract class
     *
     */
     
    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();
    } ///:~

    ... And the Controller class:

     
    //: innerclasses/controller/Controller.java
    // The reusable framework for control systems.
    // From 'Thinking in Java, 4th ed.' (c) Bruce Eckel 2005
    // www.BruceEckel.com. See copyright notice in CopyRight.txt.
     
    /***********************************************************************
     * Adapated for COMP308 Java for Programmer, 
     *		SCIS, Athabasca University
     *
     * Assignment: TME3
     * @author: Steve Leung
     * @date  : Oct 21, 2006
     *
     */
     
    package tme3;
    import java.io.FileNotFoundException;
    import java.util.*;
     
     
    public class Controller {
      // A class from java.util to hold Event objects:
      private List<Event> eventList = new ArrayList<Event>();
      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<Event>(eventList))
            if(e.ready()) {
              System.out.println(e);
              e.action();
              eventList.remove(e);
            }
      }
    } ///:~

    It also comes with 4 different text files.

    I hope this makes sense. Thanks again.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Design Model-Assignment

    Quote Originally Posted by JavaStudent100 View Post
    I hope this makes sense.
    Getting closer. What part are you working on and where are you stuck? Be specific, that is a large amount of code to dig through and the more you provide the easier it will be for people to get involved and offer advice

  5. #5
    Junior Member JavaStudent100's Avatar
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Design Model-Assignment

    Quote Originally Posted by jps View Post
    Getting closer. What part are you working on and where are you stuck? Be specific, that is a large amount of code to dig through and the more you provide the easier it will be for people to get involved and offer advice
    I totally agree that it's a lot of code to dig through and I appreciate your help. I'd like to get some help on the following:

    2. Modify Bell Event so that it will be able to run an arbitrary number of times separated by 2000 msec each. To facilitate this requirement, you should generate the number of Bell Events specified in the rings parameter in the examples file. Please pay special attention to the possibility that other events might be run in between the various Bell events.

    3. Modify Restart.action() to start the the system by reading events from a text file. Use Scanner and an appropriate regular expression.

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Design Model-Assignment

    Quote Originally Posted by JavaStudent100 View Post
    ...you should generate the number of Bell Events specified in the rings parameter in the examples file...
    Look at the example file. What does it show?
    It looks like the bell event currently does not ring any bell once. That looks like a good place to start. Get it to ring the bell once, then get it to ring multiple times. Then get it to ring an arbitrary number of times. Finally work on the 2000ms delay between rings.

  7. The Following User Says Thank You to jps For This Useful Post:

    JavaStudent100 (August 26th, 2013)

  8. #7
    Junior Member JavaStudent100's Avatar
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Design Model-Assignment

    Quote Originally Posted by jps View Post
    Look at the example file. What does it show?
    It looks like the bell event currently does not ring any bell once. That looks like a good place to start. Get it to ring the bell once, then get it to ring multiple times. Then get it to ring an arbitrary number of times. Finally work on the 2000ms delay between rings.
    This is what the file says:


    Event=ThermostatNight,time=0
    Event=LightOn,time=2000
    Event=WaterOff,time=10000
    Event=ThermostatDay,time=12000
    Event=Bell,time=9000,rings=5
    Event=WaterOn,time=6000
    Event=LightOff,time=4000
    Event=Terminate,time=20000
    Event=FansOn,time=7000
    Event=PowerOut,time=10500
    Event=FansOff,time=8000

    So I guess, I should modify the action method in the Bell class to ring and then add another method to delay?

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Design Model-Assignment

    Quote Originally Posted by JavaStudent100 View Post
    So I guess, I should modify the action method in the Bell class to ring
    Do one thing at a time is my suggestion.
    How you implement the delay between rings is:
    1) a problem to handle later
    2) depends on how the overall implementation is set up

    I have not read all of the code and tried to follow the design, but I would worry about the delay after the bell can ring

  10. #9
    Junior Member JavaStudent100's Avatar
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Design Model-Assignment

    Quote Originally Posted by jps View Post
    Do one thing at a time is my suggestion.
    How you implement the delay between rings is:
    1) a problem to handle later
    2) depends on how the overall implementation is set up

    I have not read all of the code and tried to follow the design, but I would worry about the delay after the bell can ring
    Would this makes sense:

    public class Bell extends Event {
        public Bell(long delayTime) { super(delayTime); }
        public void action() {
    	// Bell rings
        	addEvent(new Bell(5));
     
        }
        public String toString() { return "Bing!"; }
      }

  11. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Design Model-Assignment

    It looks like the constructor for a Bell object expects a value of type long which represents delayTime, not a number of rings.

    If you need to add multiple rings (or multiple events causing rings) then you would need to add more than one ring, each with their own specific start time.

  12. #11
    Junior Member JavaStudent100's Avatar
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Design Model-Assignment

    Quote Originally Posted by jps View Post
    It looks like the constructor for a Bell object expects a value of type long which represents delayTime, not a number of rings.

    If you need to add multiple rings (or multiple events causing rings) then you would need to add more than one ring, each with their own specific start time.
    Awesome, so I add them five times.

    How about question 3? I have modified the code to the following:

     public class Restart extends Event {
        public Restart(long delayTime, String filename) {
          super(delayTime);
          eventsFile = filename;
        }
     
        public void action() {
        Scanner scan= new Scanner(eventsFile);
        while(scan.hasNextLine()){
        	String line= scan.nextLine();
        }

  13. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Design Model-Assignment

    3. Modify Restart.action() to start the the system by reading events from a text file. Use Scanner and an appropriate regular expression.

    So you will have to read each line, create an event according to the data on that line, and finally add the event
    Note that special care will be required for a Bell event as there is an extra element on that line and do not forget to use a regular expression (or possibly more than one) as per the instructions

  14. #13
    Junior Member JavaStudent100's Avatar
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Design Model-Assignment

    Am I not supposed to code it in a way that the scanner reads the txt file and copies it into buffer reader or something like that?

    Thanks

Similar Threads

  1. About deleting rows from table model.
    By ZDreamer08 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 1st, 2013, 08:41 AM
  2. Model View Controller help
    By kari4848 in forum AWT / Java Swing
    Replies: 2
    Last Post: November 15th, 2012, 03:19 PM
  3. Markov model
    By KobusJordaan in forum Algorithms & Recursion
    Replies: 1
    Last Post: October 19th, 2012, 10:06 PM
  4. creating table model....
    By kollyisrealisaac in forum Java Theory & Questions
    Replies: 1
    Last Post: May 6th, 2011, 10:07 AM
  5. Unstructured Data Model
    By richip in forum Object Oriented Programming
    Replies: 1
    Last Post: March 17th, 2011, 10:25 AM