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 9 of 9

Thread: Use of await statement in problem

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Use of await statement in problem

    Hi JPF. I'm having trouble with a question given to us by our lecturer. The question is as follows

    In a toy train circuit (figure of eight) trains can enter via section 5 or section 14, do a figure of eight circuit and then leave via the same section that they entered.

    A train joining the circuit must first make sure the section ahead and the section immediately to the right/left of them is free (e.g. A train entering via section 14 must ensure the section 14 and section 13, are not occupied by another train at that time. To avoid train crashes only one train at any time can be in any of the available sections. The crossover protocol at the junction (i.e. section 0) in the centre of the figure of eight is that the first train to get access to the junction is allowed to proceed across it.

    The code that I have so far is below

    public class traintrack {
     
    	/*declare and initialise global variables */
    	final int NUMBER_OF_TRAINS= 20;
    	int numberOnTrack = 0;
     
    	//slots on the roundabout
    	String sectionsOnTrack [] = {"[.....]", "[.....]", "[.....]", "[.....]","[.....]","[.....]","[.....]","[.....]","[.....]",
    									"[.....]","[.....]","[.....]","[.....]","[.....]","[.....]","[.....]","[.....]","[.....]"};
    }
     
    	for (int count = 1; NUMBER_OF_TRAINS < 18; count++);
    	{
    		int entry = 5 & 14;
    		int exit = 5 & 14; 
     
    		trainProcess (entry, exit);
     
    	} //end for loop
    } //end main process
     
    Process trainProcess (int s, int t);
    	{ 
    		<await> (numberOnTrack <= 18, numberOnTrack++)>; //wait if track too crowded
     
    	<await(sectionsOnTrack[2..s]=="[.....]"); AND (sectionsOnTrack[2..s+7])>
     
    }	
     
    }

    the reason it cuts off around the await section is because I am rather a beginner in using java and despite being a final year student have managed to get along doing modules that are not heavily focused on coding; but also I couldn't find much on java await statements by googling.

    Any advice/nudges in the right direction would be appreciated.
    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Use of await statement in problem

    java await statements
    I don't recognize that.

    Are you asking about the Object class's wait() method?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    hcoyle545 (March 4th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Use of await statement in problem

    see thats what i'm unsure of. Our lecturer provided some code that he said would work straight out of the box for java, it was as follows:

    Global Invariant: (NumberOfTrainsInAnySection <= 1) AND (TotalTrainsOnTrack <= 8)
              process main {
    	//declare and initialise global variables 
    	Const NUMBER_OF_TRAINS=20;
    	int numberOnTrack = 0;
              slots on the roundabout 
    	string  section[0..11] = { &#8220;[.....]&#8221;, &#8220;[.....]&#8221;, &#8220;[.....]&#8221;, &#8220;[.....]&#8221;, &#8220;[.....]&#8221;, &#8220;[.....]&#8221;, &#8220;[.....]&#8221;, 
    						&#8220;[.....]&#8221;,&#8220;[.....]&#8221;, &#8220;[.....]&#8221;, &#8220;[.....]&#8221;,&#8220;[.....]&#8221; };
               //create and set the trains moving 
    	 for (count= 1 to NUMBER_OF_TRAINS){ 
                       	trainProcess (int n);
                   } /* end  for */
               } // end main process
     
               process trainProcess (int n) { 
        <await (numberOnTrack<=8) numberOnTrack++;> /*    /* Wait if track is full
         <await (section[0) ==”[.....]”)  section[0]=”[“+n+”]”;> /* Wait to get into section[0]
        /* wait for clearance before moving on to junction */
        <await ( (section[10]==&#8221;[.....]&#8221;) AND (section[1]==&#8221;[.....]&#8221;) ) section[1]= &#8221;[&#8220;+n+&#8221;]&#8221;; section[0]= &#8221;[.....]&#8221;; >
       /* move off the junction
       <await ( section[2]==”[.....]”) section[2]= ”[“+n+”]”; section[1]= ”[.....]”; >
        /* move around the loop
        int currentPosition = 2;
        do   {
              <await (section[currentPosition+1]==”[.....]”)   section[currentPosition+1]=section[currentPosition]; 
                                                                                            section[currentPosition]=”[.....]”; >
              currentPosition = currentPosition+1;
        } while (currentPosition <=10)
      /* wait for clearance before moving on to junction */
        <await ( section[1]==&#8221;[.....]&#8221;)  section[1]= &#8221;[&#8220;+n+&#8221;]&#8221;; section[10]= &#8221;[.....]&#8221;; >
        /* wait to move forward off junction */
         <await (section[11] ==&#8221;[.....]&#8221;)  section[11]=&#8221;[&#8220;+n+&#8221;]&#8221;; section [1]= &#8221;[.....]&#8221; >
        < section[11]=&#8221;[.....]&#8221;; >    /* move off the track */
        < numberOnTrack--;> /* decrement the number of trains on the track
    } /* end carProcess */

    but it doesn't work at all when I paste it in, as he says it should.

    My understanding is that the wait statement is just to ensure that there is enough room on the track for another train to enter.

    thanks for having a look at my thread too

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Use of await statement in problem

    That does not look like java code. What language is it?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Use of await statement in problem

    I agree with Norm, that doesn't look much like java.

    Is the wait command absolutely necessary? This seems like a problem about determining when the trains are to enter through the station if they are all traveling at a constant "velocity" (Sections per iteration). A wait function seems to be for a time base instead of an iterative base. There are a few controlling variables for the solution: How many sections does each train take up? How long is the figure 8 track? Most importantly, what is the distance of each station from the junction within the figure 8?

    This problem could be solved without programming it; I'd say watch/imagine a train going through the track and observe how many iterations after departing from the starting station which pass before the train enters the junction. This allows you to see the walls - moments where a collision is possible - created by any train leaving the station.

    I really like this problem, could you post the picture that goes along with it?

  7. #6
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Use of await statement in problem

    heres the picture attached... ive since found that the code I posted was psuedocode. and the aim is to make psuedocode in that same format as mentioned above for this track.

    The psuedocode I posted earlier is from a different question (just to serve as an example)

    Here's the question for the above image

    "A toy train circuit (figure of eight) is show below. Trains can enter via section 5 or section 14, do a figure of eight circuit and then leave via the same section that they entered. The route they take is indicated in figure 1.

    Traintrack.jpg

    A train joining the circuit must first make sure the section ahead and the section immediately to the right/left of them is free (e.g. A train entering via section 14 must ensure the section 14 and section 13, are not occupied by another train at that time. To avoid train crashes only one train at any time can be in any of the available sections. The crossover protocol at the junction (i.e. section 0) in the centre of the figure of eight is that the first train to get access to the junction is allowed to proceed across it.

    Assuming that shared memory is available
    (a) Design a solution using the await statement, that adhers to the above
    behaviour protocol and hence allows trains to enter, travel around and leave safely. (25%)
    (b) translate the design in part(a) into a semaphore based design. (25%)
    "


    At the minute Im working on changing the psuedocode so that it applies to the question above

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Use of await statement in problem

    Design a solution using the await statement
    There is no await statement in java. Where have you heard about an await statement?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Mar 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Use of await statement in problem

    Quote Originally Posted by Norm View Post
    There is no await statement in java. Where have you heard about an await statement?
    First time I've heard is from this question which is what's confusing. But this is just psuedocode now, it signifies waiting until a slot on the track is empty before another train can enter.

  10. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    9
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Use of await statement in problem

    Oh I see, its one of those exercises. I would start with creating objects and passing variables between them which act within the constraints. Maybe you could clear this up, is semaphore design trying to determine the memory allocation for operating this track?

    The first task of identifying the objects has already been done: TrackSegments, Trains, and Stations.
    It seems there are two different reference points for deciding how to draft the "sight" of the program. Junctions, being the point of conflict, could be created to control the trains, but seeing this exercise I am pretty sure you are supposed to focus on passing variables from TrackSegments to the Trains.

Similar Threads

  1. [SOLVED] Please help me with my String variable and switch statement problem.
    By ace1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 7th, 2013, 09:15 PM
  2. if-else statement problem! PLEASE help!
    By tlh513 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 30th, 2013, 08:48 PM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. [SOLVED] Coin Loop Selective Print Statement Problem
    By sternfox in forum Loops & Control Statements
    Replies: 1
    Last Post: January 30th, 2013, 12:30 AM
  5. Replies: 2
    Last Post: November 7th, 2012, 03:54 AM