Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:08:48+00:00 2026-06-12T18:08:48+00:00

I am trying to write a simulation for queues although I cannot see why

  • 0

I am trying to write a simulation for queues although I cannot see why I always get 0.0 at the end for all results minus the requests. It makes logical sense to me but I dont quite understand why it always ends up subtracting itself in the waitTime variable modification
Here is my results:
Numbers Served 322
Total wait time 0.0
Average wait time: 0.0

I was wondering as to why each time I generate and run the program, I dont get real values. In other words, it compiles just fine, I just dont see the mistake I made

import java.util.*;


public class IndividualQueues {

final static int simulationTimeLimit = 3600;
final static double arrivalChance = 1.0/10;
final static int numberOfServers = 5;

public static void main(String[] args) {
    Queue queueArray[] = new Queue[numberOfServers];
    int[] serviceTime = new int[numberOfServers];
    Random checkArrival = new Random();
    double totalWaitTime = 0;
    int requests = 0;
    int serviceTimer = 0;
    int waitTime = 0;
    Queue <Integer> tempQueue = new Queue<Integer>();

      for (int i=0;i<numberOfServers;i++){
       queueArray[i]= tempQueue;
      }

   for(int seconds = 1;seconds<= simulationTimeLimit;seconds++) {
       if(seconds <= simulationTimeLimit) {
           if(checkArrival.nextDouble() <= arrivalChance) {
               requests++;
                       serviceTimer = checkArrival.nextInt(49) + 10;
                       int min = queueArray[0].size();
                       for(int i = 1;i<queueArray.length;i++) {
                           int temp = queueArray[i].size();
                           if(min >temp)
                           min = temp;
                       }
                       queueArray[min].enqueue(seconds);
                       serviceTime[min] = serviceTimer;
               }


               for(int j = 0;j<serviceTime.length;j++) {
                   if(serviceTime[j] >0) {
                       serviceTime[j]--;
                     //  System.out.println(serviceTime[j]);
                   }
                   else if(serviceTime[j]== 0 && queueArray[j].size() >0) {
                       System.out.println("You got here");
                       int dequeue = (Integer)queueArray[j].dequeue();
                       waitTime = seconds - dequeue;
                       totalWaitTime += waitTime;
                   }
               }

           }
   }
   System.out.println("Numbers Served " +requests);
   System.out.println("Total wait time " +totalWaitTime);
   System.out.println("Average wait time: " +(double)totalWaitTime/requests);
}

}


public class Queue<Item> {
private int N;         // number of elements on queue
private Node first;    // beginning of queue
private Node last;     // end of queue

// helper linked list class
private class Node {
    private Item item;
    private Node next;
}

/**
  * Create an empty queue.
  */
 public Queue() {
     first = null;
     last  = null;
     N = 0;
 }

/**
  * Is the queue empty?
  */
  public boolean isEmpty() {
      return first == null;
  }

 /**
  * Return the number of items in the queue.
  */
  public int size() {
     return N;
  }

 /**
   * Return the item least recently added to the queue.
   * @throws java.util.NoSuchElementException if queue is empty.
   */
  public Item peek() {
     if (isEmpty()) throw new NoSuchElementException("Queue underflow");
     return first.item;
   }

 /**
   * Add the item to the queue.
   */
  public void enqueue(Item item) {
      Node oldlast = last;
      last = new Node();
      last.item = item;
      last.next = null;
      if (isEmpty()) first = last;
      else           oldlast.next = last;
      N++;
  }

 /**
   * Remove and return the item on the queue least recently added.
   * @throws java.util.NoSuchElementException if queue is empty.
   */
  public Item dequeue() {
      if (isEmpty()) throw new NoSuchElementException("Queue underflow");
      Item item = first.item;
      first = first.next;
      N--;
      if (isEmpty()) last = null;   // to avoid loitering
      return item;
  }    

 /* *
   * Return string representation.
   */
  public String toString() {
      StringBuilder s = new StringBuilder();
      Node current = first;
      while (current != null)
      {
          s.append(current.item + " ");
          current = current.next;
    }
      return s.toString();
  }
  }  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:08:50+00:00Added an answer on June 12, 2026 at 6:08 pm

    You’re using the same queue for all the queues in the array

    Queue <Integer> tempQueue = new Queue<Integer>();
    for (int i=0;i<numberOfServers;i++){
        queueArray[i]= tempQueue;
    }
    

    You should create each queue in the for loop:

    for (int i=0;i<numberOfServers;i++){
        //I don't know if this works
        //queueArray[i]= new Queue<Integer>();
        //LinkedList implements the Queue interface
        queueArray[i]= new LinkedList<Integer>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to write a simple Monte Carlo simulation program. To be exact,
Trying to write a function to see how often an object exists and give
Okay, I'm trying to write a ruby simulation of my grandmother. I can't quite
I'm trying to write a chess game and find that I cannot find solutions
I am trying to write a code that can get the names of the
I am trying to write a simple physics simulation where balls with varying radii
I am trying to write source based on the Game Of Life simulation concept.
I'm trying to write a driving simulation program and I need some help on
I am trying to write a simple n-body gravity simulation with 4 particles in
I am trying to write some front end Javascript tests that can test drag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.