Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\"Create a system to simulate vehicles at an intersection. Assume that there is

ID: 3599189 • Letter: #

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 collection API. 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. This means a jsjf package containing QueueADT.java file with CircularArrayQueue.java or LinkedQueue.java and LinearNode.java.

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.

Determine what is the optimal light-change time to maximize car throughput.

You can assume it takes one second for one car to cross an intersection. There are no yellow lights and no left or right turns.

Explanation / Answer

vecDriver.java:

public class vecDriver {

public static void main(String[] args)

{

vecSimulator vecsimulator = new vecSimulator();

vecsimulator.vecSimulate();

}

}

LinkedQueue.java:

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;

}

@Override

public int vecsize() {

// TODO Auto-generated method stub

return 0;

}

}

vecsimulator.java:

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

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;

public vecSimulator() {

}

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");

}

}

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);

}

}

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++;

}

}

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++;

}

}

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;

}

}

EmptyCollectionException.java:

public class EmptyCollectionException extends RuntimeException {

public EmptyCollectionException(String collection)

{

super("The " + collection + " is empty.");

}

}

QueueADT.java:

public interface QueueADT<T> {

public void vecenqueue(T element);

public T vecdequeue();

public T vecfirst();

public boolean vecisEmpty();

public int vecsize();

public String vectoString();

}

vecVehicle.java:

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;

public vecVehicle(int vehicleNum, int aTime, int dTime) {

vehicleNumber = vehicleNum;

arrivalTime = aTime;

departureTime = dTime;

direction = vecrandomDirection();

street = vecrandomStreet();

lane = vecrandomLane();

}

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;

}

public vecStreet vecrandomStreet() {

if (direction == vecDirection.N || direction == vecDirection.S) {

street = vecStreet.Church;

}

else

street = vecStreet.Main;

return street;

}

public vecLane vecrandomLane() {

int laneIndicator = (int) (Math.random() * (2 - 0) + 0);

if (laneIndicator == 1)

lane = vecLane.Left;

else

lane = vecLane.Right;

return lane;

}

public vecDirection vecgetDirection()

{

return direction;

}

public vecStreet vecgetStreet()

{

return street;

}

public vecLane vecgetLane() {

return lane;

}

public void vecsetDepartureTime(int time) {

departureTime = time;

}

public int vecgetVehicleNumber() {

return vehicleNumber;

}

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";

}

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";

}

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.";

}

}

vecLinearNode.java:

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;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote