Queue Scenario Help Getting Started

49 views Asked by At

Hi I am currently working on a queue wait time simultaion, over the course of 12 hours that adds a random number of people per line every minute while removing three from the front every minute as well. After the twelve hours are over i will average the rate in which they entered and exited the line. I need to perform this 50 times to get a more accurate model simulation. I do not currently know how to properly implement this. If i could get some pointers on where to begin it would be most appreciated. Linked List Class

public class LinkedListQueue<E>{

private Node<E> head;
private Node<E> tail;
private int size;

public LinkedListQueue() {

}

public void enqueue(E element) {
    Node newNode = new Node(element, null);

    if (size == 0) {
        head = newNode;
    } else {
        tail.setNextNode(newNode);
    }

    tail = newNode;
    size++;
}

public E dequeue() {
    if (head != null) {
        E element = head.getElement();
        head = head.getNextNode();
        size--;
        if (size == 0) {
            tail = null;
        }
        return element;
    }
    return null;
}

public E first() {
    if (head != null) {
        return head.getElement();
    }
    return null;
}

public int getSize() {
    return size;
}

public void print() {
    if (head != null) {
        Node currentNode = head;
        do {
            System.out.println(currentNode.toString());
            currentNode = currentNode.getNextNode();
        } while (currentNode != null);
    }
    System.out.println();
}
}

Node Class

public class Node<E>{
private E element;
private Node<E> next;

public Node(E element, Node next) {
    this.element = element;
    this.next = next;
}

public void setNextNode(Node next) {
    this.next = next;
}

public Node<E> getNextNode() {
    return next;
}

public E getElement() {
    return element;
}

public String toString() {
    return element.toString();
}
}

Simulation Class

import java.util.Random;

public class Simulation {

private int arrivalRate;
//you'll need other instance variables

public Simulation(int arrivalRate, int maxNumQueues) {
    this.arrivalRate = arrivalRate;
}

public void runSimulation() {
    //this is an example for using getRandomNumPeople
    //you are going to remove this whole loop.
    for (int i = 0; i < 10; i++) {
        int numPeople = getRandomNumPeople(arrivalRate);
        System.out.println("The number of people that arrived in minute " + i + " is: " + numPeople);
    }
}

//Don't change this method.
private static int getRandomNumPeople(double avg) {
    Random r = new Random();
    double L = Math.exp(-avg);
    int k = 0;
    double p = 1.0;
    do {
        p = p * r.nextDouble();
        k++;
    } while (p > L);
    return k - 1;
}

//Don't change the main method.
public static void main(String[] args) {
    Simulation s = new Simulation(18, 10);
    s.runSimulation();
}
}
1

There are 1 answers

1
mcw On

It looks like you haven't started this assignment at all.

First, start with the main() method. A new Simulation object is created. Follow the constructor call to new Simulation(18, 10). For starters, you will see that the constructor is incomplete

public Simulation(int arrivalRate, int maxNumQueues) {
    this.arrivalRate = arrivalRate;
    // missing the handling of maxNumQueues
}

So, for starters, you probably want to define a new variable of type integer (since that is what is the type of maxNumQueues according to the Simulation constructor) in the Simulation class. From there, you obviously want to get back into the constructor and set your new variable to reference the constructor call.

Example:

public class Simulation {

private int arrivalRate;
private int maxNumQueues; // keep track of the maxNumQueues

public Simulation(int arrivalRate, int maxNumQueues) {
    this.arrivalRate = arrivalRate;
    this.maxNumQueues = maxNumQueues; // initialize our new local variable maxNumQueues
}}