7 Replies Latest reply: Aug 29, 2008 3:27 PM by 843807 RSS

    Need help on an assignment

    843807
      Hello Friends, I need to solve the problem below. It is an Homework application.
      First, this is the provided stuff for the application and the requirments:
      In this project you will develop components for a vehicular traffic network simulator using the JavaBeans component framework. The system being developed is a so called discrete-event simulator, meaning that there is a central virtual clock that controls the simulation of the system. On each tick of the simulator clock, each element of the simulation is given the opportunity to do something. The clock tick interval is meant to represent an infinitesimally short time period, so the state of the system (and each component) is assumed to change only slightly during each clock tick. The simulation components also have a graphical user interface which is refreshed after each clock tick so as to provide an animation of the simulation.

      The simulation models a set of vehicles which move about a network of roads and intersections. Each type of road and intersection is implemented as a separate Java class. The simulation framework is designed to be extensible, so that new kinds of vehicles, roads and intersections can be added in the future without having to re-implement existing classes. So, each type of intersection abstracts the knowledge associated with how vehicles should safely and legally pass through that particular type of intersection. The vehicles themselves and the roads that connect them know nothing about specific types of intersections. At any given time, each vehicle is under the control of exactly one traffic component (either some form of road or some form of intersection). Vehicles can also move from one traffic component to an adjacent traffic component.

      The interaction between traffic components is abstracted by an interface called EntryPoint. In the case of a 4-way crossroad intersection, there are four separate EntryPoint interfaces that it exposes: north, east, south and west. A single lane road by comparison, has only one entry point. If vehicles can move directly from traffic component A to traffic component B, then A requires a reference to object B (of type EntryPoint) in order to communicate with it. Traffic component B doesn�t need to directly know about traffic component A, it simply receives messages from that component about vehicles wishing to enter traffic component B.

      The EntryPoint interface provides an operation called isClear which is invoked to determine if it is safe and legal for a new vehicle to enter via this entry point. Method isClear returns true if and only if it is safe and legal to enter. This decision may be influenced by the direction (left, right or straight) that the vehicle wishes to turn within the new component. The isClear operation therefore takes the turn direction as a parameter.

      The decision as to which way to turn must be made by the vehicle in question prior to arriving at the entry point. In order to make this decision, the vehicle needs to know what the possible set of turn directions are at the upcoming component (when approached from this entry point). The EntryPoint interface therefore provides a property called TurnDirections which returns a list of possible turn directions (left, right and/or straight). The vehicle class provides a method called selectTurnDirection which takes a list of possible turn direction and randomly chooses one of them. The turn direction currently selected by a vehicle is accessible via a public property called NextTurnDirection. If isClear returns false then the vehicle must remain at the end of the previous traffic component until isClear become true. If isClear returns true, then the Enter method may be called to transfer the vehicle to the new traffic component. The caller should at that point relinquish control of the vehicle.

      Some of the classes and interfaces required to implement this system have already been developed and will be supplied.

      The classes that have already been implemented are a four-way CrossRoad intersection and an abstract one Lane road with four concrete subclasses NorthLane, SouthLane, WestLane and EastLane specialized for travel in each of those directions. The EntryPoint interface and TurnDirection enumerated type have also been provided.

      Your task is to complete each of the following four development exercises.

      Part A � Generating Events

      In this part of the project you will implement a non-visual JavaBean component called SimulationClock that will control the overall simulation. Remember that a JavaBeans component is simply a plain old Java class that happens to follow a number of naming conventions. So, start by adding a new Java class called SimulationClock inside the Traffic package (just like the existing traffic components). The SimulationClock is responsible for generating the Tick events that cause each of the other traffic components to do their thing. So, the SimulationClock class will need to have a method called fireTickEvent which when called will notify all listeners that a Tick event has occurred.

      The SimulationClock will also need methods that follow the standard JavaBeans naming conventions for adding and removing listeners for simulation events. For this purpose you will need to design a new interface (called SimulationListener) that derives from java.util.EventListener. SimulationListener will need to implement a Tick method as well as another method called Reset. The Reset event is fired to instruct simulation components to reset themselves, i.e. to remove all vehicles they are currently controlling. The Tick and Reset events will need to take an event parameter of a class that derives from java.util.EventObject. Make sure you strictly follow all JavaBeans� naming and subclassing conventions for creating events, otherwise the NetBeans wizard used in the subsequent parts will not be able to automatically recognize your events.

      Note: do not change the existing CrossRoad and Lane classes in any way. In particular, do not change them to implement this new interface or change the parameters of the Tick or Reset methods (we will connect the events to these methods visually in Part C without the need for them to actually implement the listener interface).

      The SimulationClock should use the javax.swing.Timer class to help it periodically call its fireTickEvent method. The timer should be used to notify the SimulationClock itself, i.e. the SimulationClock will need to implement the ActionListener interface. The interval between ticks should be controlled by a property of the SimulationClock called Interval and its value should default to 1 millisecond. The timer should be started automatically as soon as the SimulationClock object is constructed.



      How do I complete the above Part. I created SimulationClock with a normal interval property with Set& get methods. I added ActionListener & actionPerformed() (didn't write any thing in it. I created a method fireTickEvent() & am calling in setInterval, once the ;atest interval value is set. In SimulatorListener, I added 2 methods i.e. Tick & Reset (without body) with EventObject parameter. I am getting confused, how to proceed. I believe that in SimulationClock class, NorthLane, EaseLane, WestLane, SouthLane should be members as each extends Lane which implements Tick() & it makes all vehicles present in the lane to move ahead or take turn. But am not sure and am highly confused.

      It would be good if anybody can guide me. I want to do on my own, but need some guidance. Any help is highly appreciative. Hope to get help ASAP as I need to finish this in 2 days. Please help me.

      Thanks
        • 1. Re: Need help on an assignment
          843807
          Search the forums, in the past year, I know there have been 2 full solutions posted for this problem.
          • 2. Re: Need help on an assignment
            843807
            Friend, I tried searching in the forums for last year, but couldn't find any search results except the one I have posted. Can you please refer how can I access archive posts, if possible.

            Thanks
            • 3. Re: Need help on an assignment
              843807
              Look for traffic simulation, there have been 2 or 3 grad students that have wanted to use this in their work, but haven't had a clue along with a host of undergrads.

              In any case, unless you have code and can ask specific questions about the best you're going to get is advice to read the tutorial and search the forums. Last year, this assignment was like a plague that just wouldn't go away, but is little more than a basic animation loop and a few beans.
              • 4. Re: Need help on an assignment
                843807
                I searched on net, this & other forums, but could't find nay solution or hint for this problem. Anyways, I have the source code & just got to update the code as asked for:

                In this part of the project you will implement a non-visual JavaBean component called SimulationClock that will control the overall simulation. SimulationClock is responsible for generating the Tick events that cause each of the other traffic components to do their thing. So, the SimulationClock class will need to have a method called fireTickEvent which when called will notify all listeners that a Tick event has occurred.

                The SimulationClock will also need methods that follow the standard JavaBeans naming conventions for adding and removing listeners for simulation events. For this purpose you will need to design a new interface (called SimulationListener) that derives from java.util.EventListener. SimulationListener will need to implement a Tick method as well as another method called Reset. The Reset event is fired to instruct simulation components to reset themselves, i.e. to remove all vehicles they are currently controlling. The Tick and Reset events will need to take an event parameter of a class that derives from java.util.EventObject.
                Note: do not change the existing CrossRoad and Lane classes in any way. In particular, do not change them to implement this new interface or change the parameters of the Tick or Reset methods (we will connect the events to these methods visually in Part C without the need for them to actually implement the listener interface).

                The SimulationClock should use the javax.swing.Timer class to help it periodically call its fireTickEvent method. The timer should be used to notify the SimulationClock itself, i.e. the SimulationClock will need to implement the ActionListener interface. The interval between ticks should be controlled by a property of the SimulationClock called Interval and its value should default to 1 millisecond. The timer should be started automatically as soon as the SimulationClock object is constructed

                My solution written:
                // SIMULATION EVENT CLASS
                import java.util.EventObject;
                
                public class SimulationEvent extends EventObject {
                    protected int tickInterval;
                    
                    /** Creates a new instance of SimulationEvent */
                    public SimulationEvent(Object source, int tickInterval) {
                        super(source);
                        this.tickInterval = tickInterval;
                    }
                    
                    public int getTickInterval() {
                        return this.tickInterval;
                    }
                }
                // SIMULATION LISTENER INTERFACE
                import java.util.*;
                
                public interface SimulationListener extends java.util.EventListener {
                    
                    public void Tick(SimulationEvent eventObj);
                    
                    // Remove all vehicles they are currently controlling
                    public void Reset(SimulationEvent eventObj);
                }
                
                // SIMULATION CLOCK
                public class SimulationClock implements Serializable, ActionListener {
                
                    private int interval;  // Holds value of property interval.
                    Timer timer;
                    Vector<SimulationListener> listeners;
                    
                    /** Creates a new instance of SimulationClock */
                    public SimulationClock() {
                        this.interval = 100;    // 1 millisecond
                        listeners = new Vector<SimulationListener>();
                        timer = new Timer(interval, this);
                        timer.start();
                    }
                    
                   /**
                     * Getter for property interval. - milliseconds of the interval
                     * @return Value of property interval.
                     */
                    public int getInterval() {
                        return this.interval;
                    }
                
                    /**
                     * Setter for property interval. - milliseconds of the interval
                     * @param interval New value of property interval.
                     */
                    public void setInterval(int interval) {
                        this.interval = interval;
                    }
                
                    /* fireTickEvent 
                     * 
                     * Notify all listeners that a Tick event has occured
                     */
                    public void fireTickEvent() {
                        SimulationEvent se = new SimulationEvent(this, interval);
                        synchronized(this) {
                            Vector cloneListeners = (Vector)listeners.clone();
                            Iterator iter = cloneListeners.iterator();
                            
                            while (iter.hasNext()) {
                                SimulationListener sl = (SimulationListener) iter.next();
                                sl.Tick(se);
                            }
                        }
                    }
                    
                    /* fireResetEvent 
                     * 
                     * Notify all listeners that a Tick event has occured
                     */
                    public void fireResetEvent() {
                        SimulationEvent se = new SimulationEvent(this, interval);
                        synchronized(this) {
                            Vector cloneListeners = (Vector)listeners.clone();
                            Iterator iter = cloneListeners.iterator();
                            
                            while (iter.hasNext()) {
                                SimulationListener sl = (SimulationListener) iter.next();
                                sl.Reset(se);
                            }
                        }
                    }
                     
                    /* Action Performed of ActionListener
                     */ 
                    public void actionPerformed(ActionEvent ae) {
                        // A Tick has occured 
                        fireTickEvent();
                    }
                    
                    public synchronized void addSimulationListener(SimulationListener sl) {
                        listeners.addElement(sl);
                    }
                    
                    public synchronized void removeSimulationListener(SimulationListener sl) {
                        try {
                            listeners.removeElement(sl);
                        } catch (IllegalArgumentException ie) {
                            System.out.println("ERROR: Listener to be Removed, Not Found : ");
                        }
                    }
                }
                The code I have written based on the above requiremtns is correct or not. I am confused as per this Part ofthe project :
                Do I have to implement SimulationListener in SimulationClock ?
                Is the code Correctly implemented ???

                Please solve this query. Then will proceed to other parts. I have completed all part, but somewhere I think am going wrong & not able to connect other components with this SimulationClock. All components has Tick() & Reset() with no parameters.

                Please try to help at the earliest. Now I should hurry up.

                Please. I request.

                Thanks
                • 5. Re: Need help on an assignment
                  843807
                  TruptiD wrote:
                  In this part of the project you will implement a non-visual JavaBean component called SimulationClock...
                  You have totally missed "non-visual JavaBean", but that is also what you are missing in your code to make it work.
                  • 6. Re: Need help on an assignment
                    843807
                    What am I missing, tell that. Is anything improper with Timer, actionPerformed, fireTickEvent ?? The class doesn't implement any visual thing. What is wrong give me some idea of the mistake.

                    Regards,
                    • 7. Re: Need help on an assignment
                      843807
                      Well, I told you outright what was wrong with your code and attempted implementation of the assignment and you totally missed it too. So here is a link to [Sun's Java Tutorials: The Really Big Index|http://java.sun.com/docs/books/tutorial/reallybigindex.html] for you to search through and to study. Please study carefully the section on BEANS because you apparently have no idea what they are.