Results 1 to 13 of 13
Thread: Arrays
- 10-07-2009, 07:57 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 14
- Rep Power
- 0
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
- 10-07-2009, 08:03 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Stop writing code to do with threads and start by reading Sun's tutorial from the beginning.
-
We'd be glad to help, but I don't see any specific question posted. Exactly how are you stuck?
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.Java Code:pliz highlight edited areas.
Much luck.
- 10-11-2009, 03:08 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 14
- Rep Power
- 0
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
- 10-11-2009, 03:08 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 14
- Rep Power
- 0
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
- 10-11-2009, 03:21 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 54
- Rep Power
- 0
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.
- 10-11-2009, 03:26 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 14
- Rep Power
- 0
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.
-
Well, what do you think this will do when the vehicle id is 0?
It's only behaving the way you're telling it to behave.Java Code:public synchronized void useToll(Vehicle vehicle) throws InterruptedException { while (vehicle.getVehicleId()==0) { this.wait(); }
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!
- 10-11-2009, 04:33 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 14
- Rep Power
- 0
so what should i do to make it display the output i want? give example
- 10-11-2009, 04:36 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 10-11-2009, 04:43 PM #11
Member
- Join Date
- Apr 2009
- Posts
- 14
- Rep Power
- 0
yes i hear u, but like i requested give example or show on the code i have provided,that will be very helpful
- 10-11-2009, 04:47 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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
is doing.Java Code:while (vehicle.getVehicleId()==0) { this.wait(); }
-
Why do you have that wait method that I highlighted? What is its purpose?
You may wish to scrap your current useToll method. Re-write it from the ground up.
Similar Threads
-
A little help with arrays..
By zeppelin in forum New To JavaReplies: 8Last Post: 01-05-2009, 12:33 PM -
Arrays
By TheRocket in forum New To JavaReplies: 6Last Post: 12-10-2008, 06:00 PM -
Need help with 2D arrays...
By rrsv2 in forum New To JavaReplies: 3Last Post: 11-30-2008, 03:15 AM -
Help on Arrays...
By cuellar14 in forum New To JavaReplies: 4Last Post: 07-25-2008, 08:16 PM -
arrays help
By Warren in forum New To JavaReplies: 6Last Post: 11-23-2007, 07:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks