added readme and comments for assignment
This commit is contained in:
1 parent
e4148059a5
commit
630d479739
7 files changed
+75
-1
No files matched your search
+32
@@ -0,0 +1,32 @@
|
|||||||
|
# About
|
||||||
|
|
||||||
|
This is a program that simulates traffic on a single lane bridge. All vehicles that cross the bridge implement the abstract class Vehicle. The traffic controller class manages the Semaphores and which Vehicles get to cross at what times. There's also some code in there for adding cars to the simulation. Emergency vehicles are randomly scheduled to appear at certain times during the simulation based off of the arguments.
|
||||||
|
|
||||||
|
After the program is done running it will output some information about what happened during the run of the program and some logs. The program uses Semaphores to handle what vehicle should go next, and seeing if there is a vehicle on the bridge. The vehicle class implements the runnable class and is run in a new thread when crossing the bridge for some amount of time based on the implementations.
|
||||||
|
|
||||||
|
There is a semaphore for each direction, the bridge, and emergencies.
|
||||||
|
|
||||||
|
# Compile
|
||||||
|
|
||||||
|
To compile the program you must change your directory to `src/`. You may run the following commands to compile the program.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cd src
|
||||||
|
javac *.java
|
||||||
|
```
|
||||||
|
|
||||||
|
# Running
|
||||||
|
|
||||||
|
To run the program you may run the following commands
|
||||||
|
|
||||||
|
```sh
|
||||||
|
java BridgeSimulator
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```sh
|
||||||
|
java BridgeSimulator (simulation time) (emergencies)
|
||||||
|
```
|
||||||
|
|
||||||
|
If no command line arguments are given the program will default to a simulation time of 30 seconds and 2 emergencies.
|
||||||
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Bridge Simulator : Operating Systems
|
||||||
|
*
|
||||||
|
* Program by Jackson Dean Hodge
|
||||||
|
* Student ID: 010955002
|
||||||
|
* */
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class BridgeSimulator {
|
public class BridgeSimulator {
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Bridge Simulator : Operating Systems
|
||||||
|
*
|
||||||
|
* Program by Jackson Dean Hodge
|
||||||
|
* Student ID: 010955002
|
||||||
|
* */
|
||||||
|
|
||||||
public class Car extends Vehicle {
|
public class Car extends Vehicle {
|
||||||
public int crossingTime = 2;
|
public int crossingTime = 2;
|
||||||
public static int priority = 2;
|
public static int priority = 2;
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Bridge Simulator : Operating Systems
|
||||||
|
*
|
||||||
|
* Program by Jackson Dean Hodge
|
||||||
|
* Student ID: 010955002
|
||||||
|
* */
|
||||||
|
|
||||||
public class EmergencyVehicle extends Vehicle {
|
public class EmergencyVehicle extends Vehicle {
|
||||||
public int crossingTime = 1;
|
public int crossingTime = 1;
|
||||||
public static int priority = 1;
|
public static int priority = 1;
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Bridge Simulator : Operating Systems
|
||||||
|
*
|
||||||
|
* Program by Jackson Dean Hodge
|
||||||
|
* Student ID: 010955002
|
||||||
|
* */
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Bridge Simulator : Operating Systems
|
||||||
|
*
|
||||||
|
* Program by Jackson Dean Hodge
|
||||||
|
* Student ID: 010955002
|
||||||
|
* */
|
||||||
|
|
||||||
public class Truck extends Vehicle {
|
public class Truck extends Vehicle {
|
||||||
public int crossingTime = 3;
|
public int crossingTime = 3;
|
||||||
public static int priority = 2;
|
public static int priority = 2;
|
||||||
|
|||||||
+8
-1
@@ -1,3 +1,10 @@
|
|||||||
|
/*
|
||||||
|
* Bridge Simulator : Operating Systems
|
||||||
|
*
|
||||||
|
* Program by Jackson Dean Hodge
|
||||||
|
* Student ID: 010955002
|
||||||
|
* */
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.Semaphore;
|
import java.util.concurrent.Semaphore;
|
||||||
|
|
||||||
@@ -56,7 +63,7 @@ public abstract class Vehicle implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(this.crossingTime);
|
Thread.sleep(this.getCrossingTime());
|
||||||
hasRun = true;
|
hasRun = true;
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
Reference in new issue
Block a user