0

I am having trouble looping through all of my questions that have been assigned to each oracle's queue. All of the questions are being added successfully, but I cannot for the life of me have each oracle answer each questions that it has been assigned 1 by 1. Meaning that oracle 1 answers its first question, oracle 2 answers its first questions and so on, until all queues are empty and all questions have been answered.

/*
 * Name: James Combs
 * Course: 3345 Data Structures and Algorithms
 * Description:
 *  This program assigns array queues to a number of oracles and loops through them in a 
 *  round robin fashion, answering each question the oracle as in its queue.
 */

package OracleQueues;

import java.util.Iterator;
import java.util.Random;

public class Executor 
{

    public static void addQuestions (String[] questions, ArrayQueue[] oracles, int numOracles)
    {   
        // Put the questions into a random oracles queue
        for (int i = 0; i < questions.length; i++)
        {
            try 
            {
                int rand = Utility.random(numOracles);

                if (oracles[rand] == null)
                {
                    System.out.println("Oracle " + (rand + 1) + " does not have a queue");
                    ArrayQueue queue = new ArrayQueue();
                    oracles[rand] = queue;
                    oracles[rand].enqueue(questions[i]);
                    System.out.println("Oracle " + (rand + 1) + " has added question ----(" + (i + 1) + ")" + questions[i] + "---- to its queue");
                }
                else
                {
                    oracles[rand].enqueue(questions[i]);
                    System.out.println("Oracle " + (rand + 1) + " has added question ---- (" + (i + 1) + ")" + questions[i] + "---- to its queue");
                }
            }
            catch (IllegalStateException e)
            {
                // advance to next oracle if this oracles queue is full, until questions have all been assigned.
                System.out.println("This oracles queue is full, advancing to the next one...");
                continue;
            }
        }
    }

    public static void main(String[] args) 
    {
        Utility.init();                                 // initializes file readers
        String[] questions = Utility.readQuestions();   // reads question.txt file into questions array
        String[] answers = Utility.readAnswers();       // reads answers.txt file into answers array
        int oCount = 0, qCount = 0;
        int numOracles = answers.length;                // finds the number of oracles
        ArrayQueue[] oracles = new ArrayQueue[numOracles];      // initialize the number of oracles based on number of answers in the file

        // 10 oracles

        addQuestions(questions, oracles, numOracles);

        System.out.println("\n\n");

        // Loop through the oracles, having each one remove a question from its queue (if empty do nothing) and answer it with its unique answer (oracle[k] uses answers[k]). Do this repeatedly till all queues become empty.
        // Create a list to hold the oracles

        for(String q : questions)
        {
            if (oCount == 10)
            {
                oCount = 0;
            }

            if (oracles[oCount] == null || oracles[oCount].isEmpty())
            {
                continue;
            }

            String output = oracles[oCount].dequeue();

            System.out.println("Oracle # " + (oCount + 1) + " ---- " + output + " ----> " + answers[oCount]);
            oCount++;
        }

        /*for (int i = 0; i < questions.length; i++)
        {
            System.out.println("Answering question " + (i + 1) + "...");

            for (int j = 0; j < numOracles; j++)
            {
                // if queue is empty or doesnt have one, then continue
                if (oracles[j] == null || oracles[j].isEmpty())
                {
                    continue;
                }

                // otherwise, dequeue and display question and corresponding answer
                String output = oracles[j].dequeue();

                System.out.println("Oracle # (" + (j + 1) + ") --- " + output + " ----> " + answers[j] + "\n");
            }*/

        }
}
James Combs
  • 324
  • 1
  • 3
  • 15
  • 1
    Why do you do the random placement in the first place? Round robin is literally just a for loop over the queues. – Thomas Jungblut Feb 24 '15 at 22:00
  • It is part of simulating a scheduling process. I figured it out eventually. Thanks. There was something slightly wrong with my ArrayQueue class – James Combs Feb 26 '15 at 20:45

1 Answers1

0

Questions has ben solved after lots of thought. Something was slightly wrong with my ArrayQueue class

James Combs
  • 324
  • 1
  • 3
  • 15