Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 19th, 2003, 11:39 AM
mike_lawson mike_lawson is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 2 mike_lawson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy i need help

hi im a new to java and having some problem with understanding some code, and would very much appreciate it if you could help me. my lecturer has given us the piece of code and asked us to explain it. im puttin in the piece of code in below could you please comment it and give a brief discription of how it works i.e. what are the classes the methods where mutual exclusion is used etc etc.

package traf;

import java.util.Vector;

import java.util.Random;

public class traf {

public static void main (String[] args) throws Exception{
new traf ();
}
public traf() {

City myCity = new City (50);


VehicleMaker vMaker = new VehicleMaker(myCity,50);
CheckPoint cp;
Vector q;

for(int i=0;i<10;i++) {
cp = new CheckPoint("CheckPoint "+i,myCity);
myCity.addCheckPoint(cp);
}
vMaker.start();
myCity.startCheckPoints();

}

private class City {

private int mNumVehicle;
private int mMaxNumVehicle;
private Vector mCheckPointList;
private Vector mVehicleList;

public Object mLock;

public City (int m) {
mNumVehicle = 0;
mMaxNumVehicle = m;
mCheckPointList = new Vector();
mVehicleList = new Vector();

}

public void vehicleEnters(Vehicle v) {
mNumVehicle++;
if(mNumVehicle >= mMaxNumVehicle) {
swapCheckPointSign();
}
System.out.println("Vechicle(s) in the City(Enter): "+mNumVehicle);
for(int i=mVehicleList.size()-1;i >= 0;i--) {
if(!((Vehicle)mVehicleList.elementAt(i)).isAlive() ) {
mVehicleList.remove(i);
}
}
v.start();
mVehicleList.addElement(v);
}

public void vehicleLeaves() {
mNumVehicle--;
if(mNumVehicle == mMaxNumVehicle-1) {
swapCheckPointSign();
}
System.out.println("Vehicle(s) in the City(on Exit): "+mNumVehicle);
if(mNumVehicle<0) {
throw new RuntimeException("Number of Vehicles"+ mNumVehicle);
}
}

public Vector getCheckPoints() {
return mCheckPointList;
}

public void addCheckPoint(CheckPoint pCheckPoint) {
mCheckPointList.add(pCheckPoint);
}

public void swapCheckPointSign() {
for(int i=0; i < mCheckPointList.size();i++) {
((CheckPoint)mCheckPointList.elementAt(i)).swapSig n();
}
}

public void startCheckPoints() {
for(int i=0; i < mCheckPointList.size();i++) {
((CheckPoint)mCheckPointList.elementAt(i)).start() ;
}
swapCheckPointSign();
}
}


private class CheckPoint extends Thread {
private String msName;
private boolean mbIsGreen;
private Vector mVQueue;
private City mCity;

public CheckPoint (String psName,City c) {
msName = psName;
mbIsGreen = false;
mVQueue = new Vector();
mCity = c;
}

public synchronized void vehicleLeaves(Vehicle v) {
synchronized(mCity) {
System.out.println(msName+": "+v+ " Leaves.");
mCity.vehicleLeaves();
}
}

public void addVechicleToQueue(Vehicle v) {
synchronized(mVQueue) {
mVQueue.addElement(v);
}
}
public Vehicle getNextVechicle() {
Vehicle v=null;
synchronized(mVQueue) {
if(mVQueue.size() > 0) {
v = (Vehicle)mVQueue.remove(0);
}
}
return v;
}
public void swapSign() {
mbIsGreen = !mbIsGreen;
}
public void run () {
Vehicle v=null;
while (true) {
if(v==null) {
v = getNextVechicle();
}
if( v != null ) {
synchronized(mCity) {
if(mbIsGreen) {
System.out.println(msName+": "+v + " Enters.");
mCity.vehicleEnters(v);
v=null;
}
}
}
}
}
}

private class Vehicle extends Thread {
private String msPlate;
private int needTime;
private CheckPoint mcpExit;

public Vehicle (String psPlate, int t,CheckPoint cp) {
msPlate = psPlate;
needTime = t;
mcpExit = cp;
}

public void run () {
try {
sleep(needTime);
} catch (InterruptedException e) {
System.err.println(msPlate + " - Person Interrupted");
}
mcpExit.vehicleLeaves(this);
}

public String toString() {
return msPlate;
}
}

private class VehicleMaker extends Thread {
private Random mRCheckPoint;
private Random mRTime;
private Random mRDelay;
private City city;
private int numVehicle;

public VehicleMaker(City c, int n) {
mRCheckPoint = new Random();
mRTime = new Random();
mRDelay = new Random();
numVehicle = n;
city = c;
}

public void run () {
int rq;
int rs;
int rd;
int iEntryCheckPoint;
int iExitCheckPoint;
Vector q = city.getCheckPoints();
CheckPoint cp;
for (int i=0;i<numVehicle;i++) {
iEntryCheckPoint = Math.abs(mRCheckPoint.nextInt()) % q.size();
iExitCheckPoint = Math.abs(mRCheckPoint.nextInt()) % q.size();
cp = (CheckPoint) q.elementAt(iEntryCheckPoint);
cp.addVechicleToQueue(new Vehicle("Vehicle "+i,Math.abs(mRTime.nextInt()%50),(CheckPoint) q.elementAt(iExitCheckPoint)));
System.out.println("Made Vehicle "+i);
try {
sleep(Math.abs(mRDelay.nextInt())%10);
} catch (InterruptedException e) {
System.err.println("People making Interrupted");
}
}
}
}


}

Reply With Quote
  #2  
Old December 19th, 2003, 01:13 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via ICQ to dhouston
I don't know a great deal about java, but if you've got any English papers you need written, I'm up for it.

Reply With Quote
  #3  
Old December 19th, 2003, 04:47 PM
mike_lawson mike_lawson is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 2 mike_lawson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
all i need is the code commented and a brief discription of what is happening. the program is bassically to develop a Java application programme that makes use of
the some of the concurrency features of the Java language: threads and mutual exclusion.

Task Specification
A cityís local authority is looking to manage the congestion caused by too many vehicles
entering the city, by limiting the total number of vehicles that can be present within the city
walls at any one time. It will achieve this by counting vehicles entering and leaving the city
using sensors built into the road at checkpoints around the city perimeter. Traffic lights will
be used to restrict access to the city at each checkpoint, changing from green to red when the
total number of vehicles within the walls exceeds some pre-determined maximum.

Reply With Quote
  #4  
Old December 26th, 2003, 12:52 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,886 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 18
Um... are you getting an error with this code?
Or do you expect us to do you homework for you?



Give a man a fish, he'll eat for a day... Teach a man to fish, he'll eat forever

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > i need help


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap