Create a system to simulate vehicles at an intersection. Assume that there is on
ID: 663799 • Letter: C
Question
Create a system to simulate vehicles at an intersection. Assume that there is one lane going in each of four directions, with stoplights facing each direction. Vary the arrival average of vehicles in each direction and the frequency of the light changes to view the "behavior" of the intersection.
You canNOT use the Java built-in Queue or Deque class. Use an Array or Linked List implementation of a queue as explained in the text.
Include user-defined exceptions and javadoc comments.
Be sure to test thoroughly, and document your results.
Be sure to label your outputs so that when I run your code I can readily understand what you're doing.
Explanation / Answer
Program:
vecSimulator:
import java.lang.Math;
import java.io.*;
import java.util.*;
import java.net.*;
import java.io.*;
public class vecSimulator
{
private LinkedQueue<vecVehicle> EL = new LinkedQueue<vecVehicle>();
private LinkedQueue<vecVehicle> ER = new LinkedQueue<vecVehicle>();
private LinkedQueue<vecVehicle> WL = new LinkedQueue<vecVehicle>();
private LinkedQueue<vecVehicle> WR = new LinkedQueue<vecVehicle>();
private LinkedQueue<vecVehicle> NL = new LinkedQueue<vecVehicle>();
private LinkedQueue<vecVehicle> NR = new LinkedQueue<vecVehicle>();
private LinkedQueue<vecVehicle> SCL = new LinkedQueue<vecVehicle>();
private LinkedQueue<vecVehicle> SCR = new LinkedQueue<vecVehicle>();
int time = 0;
int i = 0;
int veh_num = 1;
FileWriter fw;
BufferedWriter bw;
PrintWriter Fileout;
// Constructor
public vecSimulator() {}
// Method to call other method and write the result to file
public void vecSimulate() {
try {
fw = new FileWriter("output.txt");
bw = new BufferedWriter(fw);
Fileout = new PrintWriter(bw);
Fileout.print("---Start of simulation, time set to 0.--- ");
vecpopulate((int)(Math.random() * (13 - 7) + 7));
while(!vecqueuesEmpty()) {
Fileout.print("---Light changed. Now processing north/south-bound traffic--- ");
vecmoveNorthSouth();
vecpopulate((int)(Math.random() * (16 - 8) + 8));
Fileout.println();
Fileout.print("---Light changed. Now processing east/west-bound traffic--- ");
vecmoveEastWest();
vecpopulate((int)(Math.random() * (16 - 3) + 3));
Fileout.println();
}
Fileout.close();
}
catch(IOException e){
System.err.println("Error printing to file");
}
}
// Method to populate the inter section
private void vecpopulate(int randomNum) {
int count = 0;
while (count < randomNum && veh_num <=120) {
vecVehicle car = new vecVehicle(veh_num, time, time);
count++;
veh_num++;
if (car.vecgetStreet() == vecVehicle.vecStreet.Main && car.vecgetDirection() == vecVehicle.vecDirection.E && car.vecgetLane() == vecVehicle.vecLane.Left)
EL.vecenqueue(car);
else if (car.vecgetStreet() == vecVehicle.vecStreet.Main && car.vecgetDirection() == vecVehicle.vecDirection.E && car.vecgetLane() == vecVehicle.vecLane.Right)
ER.vecenqueue(car);
else if (car.vecgetStreet() == vecVehicle.vecStreet.Main && car.vecgetDirection() == vecVehicle.vecDirection.W && car.vecgetLane() == vecVehicle.vecLane.Left)
WL.vecenqueue(car);
else if (car.vecgetStreet() == vecVehicle.vecStreet.Main && car.vecgetDirection() == vecVehicle.vecDirection.W && car.vecgetLane() == vecVehicle.vecLane.Right)
WR.vecenqueue(car);
else if (car.vecgetStreet() == vecVehicle.vecStreet.Church && car.vecgetDirection() == vecVehicle.vecDirection.N && car.vecgetLane() == vecVehicle.vecLane.Left)
NL.vecenqueue(car);
else if (car.vecgetStreet() == vecVehicle.vecStreet.Church && car.vecgetDirection() == vecVehicle.vecDirection.N && car.vecgetLane() == vecVehicle.vecLane.Right)
NR.vecenqueue(car);
else if (car.vecgetStreet() == vecVehicle.vecStreet.Church && car.vecgetDirection() == vecVehicle.vecDirection.S && car.vecgetLane() == vecVehicle.vecLane.Left)
SCL.vecenqueue(car);
else
SCR.vecenqueue(car);
}
}
// Method to simulate the vehicle movement
private void vecmoveNorthSouth() {
int i = 0;
while (i < 2) {
time+=3;
try {
if (!NL.vecisEmpty()) {
vecVehicle car = new vecVehicle(0,0,0);
car = NL.vecdequeue();
car.vecsetDepartureTime(time);
Fileout.println(car);
}
}
catch(EmptyCollectionException e) {}
try {
if (!NR.vecisEmpty()) {
vecVehicle car = new vecVehicle(0,0,0);
car = NR.vecdequeue();
car.vecsetDepartureTime(time);
Fileout.println(car);
}
}
catch(EmptyCollectionException e) {}
try {
if (!SCL.vecisEmpty()) {
vecVehicle car = new vecVehicle(0,0,0);
car = SCL.vecdequeue();
car.vecsetDepartureTime(time);
Fileout.println(car);
}
}
catch(EmptyCollectionException e) {}
try {
if (!SCR.vecisEmpty()) {
vecVehicle car = new vecVehicle(0,0,0);
car = SCR.vecdequeue();
car.vecsetDepartureTime(time);
Fileout.println(car);
}
}
catch(EmptyCollectionException e) {}
i++;
}
}
// Method to move the vehicle in east west
private void vecmoveEastWest() {
int i = 0;
while (i < 3) {
time+=3;
try {
if (!EL.vecisEmpty()) {
vecVehicle car = new vecVehicle(0,0,0);
car = EL.vecdequeue();
car.vecsetDepartureTime(time);
Fileout.println(car);
}
}
catch(EmptyCollectionException e) {}
try {
if (!ER.vecisEmpty()) {
vecVehicle car = new vecVehicle(0,0,0);
car = ER.vecdequeue();
car.vecsetDepartureTime(time);
Fileout.println(car);
}
}
catch(EmptyCollectionException e) {}
try {
if (!WL.vecisEmpty()) {
vecVehicle car = new vecVehicle(0,0,0);
car = WL.vecdequeue();
car.vecsetDepartureTime(time);
Fileout.println(car);
}
}
catch(EmptyCollectionException e) {}
try {
if (!WR.vecisEmpty()) {
vecVehicle car = new vecVehicle(0,0,0);
car = WR.vecdequeue();
car.vecsetDepartureTime(time);
Fileout.println(car);
}
}
catch(EmptyCollectionException e) {}
i++;
}
}
// Method to empty the queue
private boolean vecqueuesEmpty(){
boolean empty;
empty = EL.vecisEmpty();
if(empty)
empty = ER.vecisEmpty();
if(empty)
empty = WL.vecisEmpty();
if(empty)
empty = WR.vecisEmpty();
if(empty)
empty = NL.vecisEmpty();
if(empty)
empty = NR.vecisEmpty();
if(empty)
empty = SCL.vecisEmpty();
if(empty)
empty = SCR.vecisEmpty();
return empty;
}
}
vecVehicle:
public class vecVehicle {
public enum vecDirection{N, E, S, W};
public enum vecStreet{Main, Church};
public enum vecLane{Left, Right};
private int vehicleNumber;
private int arrivalTime;
private int departureTime;
private vecStreet street;
private vecDirection direction;
private vecLane lane;
private String bound;
private String continuation;
// Constructor
public vecVehicle (int vehicleNum, int aTime, int dTime) {
vehicleNumber = vehicleNum;
arrivalTime = aTime;
departureTime = dTime;
direction = vecrandomDirection();
street = vecrandomStreet();
lane = vecrandomLane();
}
// Method to assign the direction randomly
public vecDirection vecrandomDirection() {
int dirIndicator = (int)(Math.random() * (4 - 0) + 0);
if(dirIndicator == 1)
direction = vecDirection.N;
else if(dirIndicator == 2)
direction = vecDirection.E;
else if(dirIndicator == 3)
direction = vecDirection.S;
else
direction = vecDirection.W;
return direction;
}
// Randomly assign the vehicle street based
public vecStreet vecrandomStreet() {
if(direction == vecDirection.N || direction == vecDirection.S) {
street = vecStreet.Church;
}
else
street = vecStreet.Main;
return street;
}
// Method to assign the vehicle in randomly generated lane
public vecLane vecrandomLane() {
int laneIndicator = (int)(Math.random() * (2 - 0) + 0);
if (laneIndicator ==1)
lane =vecLane.Left;
else
lane = vecLane.Right;
return lane;
}
// Accessors method to get the direction
public vecDirection vecgetDirection()
{
return direction;
}
// Accessors method to view the street of a car object.
public vecStreet vecgetStreet()
{
return street;
}
// Accessors method to view the lane of a car object.
public vecLane vecgetLane() {
return lane;
}
// Accessors method to set the departure.
public void vecsetDepartureTime(int time){
departureTime = time;
}
// Accessors method to get number.
public int vecgetVehicleNumber() {
return vehicleNumber;
}
// Method for associate direction
public String vecgetBound() {
if (direction == vecDirection.S)
return "southbound";
else if (direction == vecDirection.N)
return "northbound";
else if (direction == vecDirection.W)
return "westbound";
else
return"eastbound";
}
// Method to define the continuation
private String vecgetContinuation() {
if (lane == vecLane.Left)
return "continued straight";
else if (lane ==vecLane.Right && direction == vecDirection.S)
return "turned right and headed westbound";
else if (lane ==vecLane.Right && direction == vecDirection.N)
return "turned right and headed eastbound";
else if (lane ==vecLane.Right && direction == vecDirection.W)
return "turned right and headed northbound";
else
return "turned right and headed southbound";
}
// Method to represent string object
public String vectoString() {
String waittime = String.format("%02d",(departureTime - arrivalTime));
return "[Time " + String.format("%02d", departureTime) + "] Vehicle #" + vehicleNumber + " (" + vecgetBound() + ") " + vecgetContinuation() + ". Total wait time " + waittime + " seconds.";
}
}
vecDriver:
public class vecDriver
{
public static void main (String[] args)
{
vecSimulator vecsimulator = new vecSimulator();
vecsimulator.vecSimulate();
}
}
EmptyCollectionException:
public class EmptyCollectionException extends RuntimeException
{
public EmptyCollectionException(String collection)
{
super("The " + collection + " is empty.");
}
}
vecLinearNode:
public class vecLinearNode<T>
{
private vecLinearNode<T> next;
private T element;
public vecLinearNode()
{
next = null;
element = null;
}
public vecLinearNode(T elem)
{
next = null;
element = elem;
}
public vecLinearNode<T> vecgetNext()
{
return next;
}
public void vecsetNext(vecLinearNode<T> node)
{
next = node;
}
public T vecgetElement()
{
return element;
}
public void vecsetElement(T elem)
{
element = elem;
}
}
LinkedQueue:
public class LinkedQueue<T> implements QueueADT<T>
{
private int count;
private vecLinearNode<T> head, tail;
public LinkedQueue()
{
count = 0;
head = tail = null;
}
public void vecenqueue(T element)
{
vecLinearNode<T> node = new vecLinearNode<T>(element);
if (vecisEmpty())
head = node;
else
tail.vecsetNext(node);
tail = node;
count++;
}
public T vecdequeue() throws EmptyCollectionException
{
if (vecisEmpty())
throw new EmptyCollectionException("queue");
T result = head.vecgetElement();
head = head.vecgetNext();
count--;
if (vecisEmpty())
tail = null;
return result;
}
public T vecfirst() throws EmptyCollectionException
{
if(!vecisEmpty())
return(head.vecgetElement());
else
throw new EmptyCollectionException("Nothing to peek");
}
public boolean vecisEmpty()
{
return(count == 0);
}
public int size()
{
return(count);
}
public String vectoString()
{
String queueString = "";
try
{
vecLinearNode<T> elem = head;
T elem2 = elem.vecgetElement();
for(int i = count; i > 0; i--)
{
queueString += (elem2);
if(i > 1)
{
queueString += " ";
elem = elem.vecgetNext();
elem2 = elem.vecgetElement();
}
}
}
catch(NullPointerException e)
{
System.err.println("The tail is empty");
}
return queueString;
}
}
QueueADT
public interface QueueADT<T>
{
public void vecenqueue(T element);
public T vecdequeue();
public T vecfirst();
public boolean vecisEmpty();
public int vecsize();
public String vectoString();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.