added readme and comments for assignment

This commit is contained in:
JISAUAY committed 2024-10-10 15:02:24 -05:00
1 parent e4148059a5
commit 630d479739
7 files changed
+75 -1

No files matched your search

+32
View File
@@ -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.
+7
View File
@@ -1,3 +1,10 @@
/*
* Bridge Simulator : Operating Systems
*
* Program by Jackson Dean Hodge
* Student ID: 010955002
* */
import java.util.*;
public class BridgeSimulator {
+7
View File
@@ -1,3 +1,10 @@
/*
* Bridge Simulator : Operating Systems
*
* Program by Jackson Dean Hodge
* Student ID: 010955002
* */
public class Car extends Vehicle {
public int crossingTime = 2;
public static int priority = 2;
+7
View File
@@ -1,3 +1,10 @@
/*
* Bridge Simulator : Operating Systems
*
* Program by Jackson Dean Hodge
* Student ID: 010955002
* */
public class EmergencyVehicle extends Vehicle {
public int crossingTime = 1;
public static int priority = 1;
+7
View File
@@ -1,3 +1,10 @@
/*
* Bridge Simulator : Operating Systems
*
* Program by Jackson Dean Hodge
* Student ID: 010955002
* */
import java.util.*;
import java.util.concurrent.Semaphore;
+7
View File
@@ -1,3 +1,10 @@
/*
* Bridge Simulator : Operating Systems
*
* Program by Jackson Dean Hodge
* Student ID: 010955002
* */
public class Truck extends Vehicle {
public int crossingTime = 3;
public static int priority = 2;
+8 -1
View File
@@ -1,3 +1,10 @@
/*
* Bridge Simulator : Operating Systems
*
* Program by Jackson Dean Hodge
* Student ID: 010955002
* */
import java.util.Random;
import java.util.concurrent.Semaphore;
@@ -56,7 +63,7 @@ public abstract class Vehicle implements Runnable {
@Override
public void run() {
try {
Thread.sleep(this.crossingTime);
Thread.sleep(this.getCrossingTime());
hasRun = true;
} catch (InterruptedException e) {
e.printStackTrace();