Results 1 to 13 of 13

Thread: Arrays

  1. #1
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default Arrays

    hi guys am trying to edit the main method so that i can create an array of five vehicles(as separate threads). The vehicles should be given IDs ),1,2,3 and 4. now am stuck i need help,pliz highlight edited areas. Thanx in advance. Here is the code i did below.

    Java Code:
    import java.io.*;
    
    public class Simulate
    {
        private static int noOfVehicles = 5;
        private static Vehicle vehicles[];
    
        public static void main(String[] args) 
        {
           /*int [] VehicleId = new int[5];
           VehicleId[0] = 1;
           VehicleId[1] = 2;
           VehicleId[2] = 3;
           VehicleId[3] = 4;
           VehicleId[4] = 5;
          */
    
            // Vehicle vehicles[] = new Vehicle();
    
           Thread t1 = new vehicleId(VehicleId);
           Thread t2 = new vehicleId(VehicleId);    
           Thread t3 = new vehicleId(VehicleId);
           Thread t4 = new vehicleId(VehicleId);
           Thread t5 = new vehicleId(VehicleId);
    
           t1.start();
           t2.start();
           t3.start();
           t4.start();
           t5.start();
    
           //System.out.flush(); // make sure all output appears
        }
    }
    Last edited by Fubarable; 10-11-2009 at 04:21 PM. Reason: Code tags added for readability

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Stop writing code to do with threads and start by reading Sun's tutorial from the beginning.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by karabo101 View Post
    hi guys am trying to edit the main method so that i can create an array of five vehicles(as separate threads). The vehicles should be given IDs ),1,2,3 and 4. now am stuck i need help,
    We'd be glad to help, but I don't see any specific question posted. Exactly how are you stuck?

    Java Code:
    pliz highlight edited areas.
    I'm afraid that this isn't a "fix my code for me, and highlight the changes for me" service. Again, please ask a specific answerable question and we'll be glad to try to help.

    Much luck.

  4. #4
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    hi all, i want to allow each one of the vehicles according to their ID numbers to enter the toll booth regardless of their order of arrival at the toll booth. Below is my code


    Java Code:
    package tma01_q3_2009h;
    /*
     * Tollbooth.java
     *
     *  
     *
     * Created on 19 May 2009, 13:48
     *
     * M362 CourseTeam.
     */
    
    public class TollBooth
    {
        protected int VehicleId;
          protected Vehicle travel;
    
       public TollBooth() 
       {
        // no data fields
       }
              
       public synchronized void useToll(Vehicle vehicle) throws InterruptedException
       {
                   while (vehicle.getVehicleId()==0)
            {
                this.wait();
            }
            int [] VehicleId = new int [5];
            for (int j=0; j < VehicleId.length;j++)
            {
            /* VehicleId[j]=j;*/
           vehicle.travel(50);
           System.out.println("Vehicle" + vehicle.getVehicleId() + " enters toll booth");
           
           System.out.println("Vehicle" + vehicle.getVehicleId() +  " exits toll booth");
                 
           this.notifyAll();
       }
                
       }
    }
    Last edited by Fubarable; 10-11-2009 at 04:19 PM. Reason: Code tags added for readability

  5. #5
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    hi all, i want to allow each one of the vehicles according to their ID numbers to enter the toll booth regardless of their order of arrival at the toll booth. Below is my code


    Java Code:
    package tma01_q3_2009h;
    /*
     * Tollbooth.java
     *
     *  
     *
     * Created on 19 May 2009, 13:48
     *
     * M362 CourseTeam.
     */
    
    public class TollBooth
    {
        protected int VehicleId;
          protected Vehicle travel;
    
       public TollBooth() 
       {
        // no data fields
       }
              
       public synchronized void useToll(Vehicle vehicle) throws InterruptedException
       {
                   while (vehicle.getVehicleId()==0)
            {
                this.wait();
            }
            int [] VehicleId = new int [5];
            for (int j=0; j < VehicleId.length;j++)
            {
            /* VehicleId[j]=j;*/
           vehicle.travel(50);
           System.out.println("Vehicle" + vehicle.getVehicleId() + " enters toll booth");
           
           System.out.println("Vehicle" + vehicle.getVehicleId() +  " exits toll booth");
                 
           this.notifyAll();
       }
                
       }
    }
    Last edited by Fubarable; 10-11-2009 at 04:18 PM. Reason: Code tags added for readability

  6. #6
    pellebye is offline Member
    Join Date
    Apr 2009
    Posts
    54
    Rep Power
    0

    Default

    Java Code:
    {
     i want to allow each one of the vehicles according to their ID numbers to
     enter the toll booth regardless of their order of arrival at the toll booth.
    }

    Yes, that is what you want ... but what is your problem?
    -
    Life is not the worst thing we have ... in a few minutes my coffee is ready.

  7. #7
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default java

    The problem is vehicle 0 does not enter the tollbooth, it only starts the journey and arrives without entering the tollbooth. other vehicles are ding fine only vehicle 0.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by karabo101 View Post
    The problem is vehicle 0 does not enter the tollbooth, it only starts the journey and arrives without entering the tollbooth. other vehicles are ding fine only vehicle 0.
    Well, what do you think this will do when the vehicle id is 0?
    Java Code:
       public synchronized void useToll(Vehicle vehicle) throws InterruptedException
       {
            while (vehicle.getVehicleId()==0)
            {
                this.wait();
            }
    It's only behaving the way you're telling it to behave.

    Also, I've added code tags to your previous posts to make the posted code more readable. I suggest you do the same with future posts. Much luck!

  9. #9
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    so what should i do to make it display the output i want? give example

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read Sun's Java tutorial from the start like I suggested.
    Programming is not about shooting in the dark. The behaviors of the while construct and the wait method are all well explained in there.

  11. #11
    karabo101 is offline Member
    Join Date
    Apr 2009
    Posts
    14
    Rep Power
    0

    Default

    yes i hear u, but like i requested give example or show on the code i have provided,that will be very helpful

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You don't need any code snippets to copy and paste. You need to understand the code you already have. You need to understand what
    Java Code:
    while (vehicle.getVehicleId()==0) {
          this.wait();
    }
    is doing.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. A little help with arrays..
    By zeppelin in forum New To Java
    Replies: 8
    Last Post: 01-05-2009, 12:33 PM
  2. Arrays
    By TheRocket in forum New To Java
    Replies: 6
    Last Post: 12-10-2008, 06:00 PM
  3. Need help with 2D arrays...
    By rrsv2 in forum New To Java
    Replies: 3
    Last Post: 11-30-2008, 03:15 AM
  4. Help on Arrays...
    By cuellar14 in forum New To Java
    Replies: 4
    Last Post: 07-25-2008, 08:16 PM
  5. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •