0

I am working on a project where i am trying to implement a queue using an array. In the queue will be a list of people with some information attached. I was looking to get help on creating an array of that type along with how it would be manipulated. Three programs below are a Driver, a class to initialize the person, and a class to manipulate the queue while reading from a file.

package queue;

public class soldierQueue{

   private String Name, Branch, Duty;
   private int Commitment; 


   public String soldierQueue(String name, String branch, String duty, int commitment){
   Name = name;
   Branch = branch;
   Duty = duty;
   Commitment = commitment;

   String soldierInfo = Name  + ", " + Branch + ", " + Duty + ", " + Commitment;  



   return soldierInfo;

   }

 }


package queue;

public class QueueNGobernClass {
   public int front = 0, rear = 0,count = 0,size = 5;
   public soldierQueue[] soldiers = new soldierQueue[20];

    //adds to the rear of the queue
    public int[] addPerson(int[] queue,int addNumber){

        if(count == size){
            System.out.println("stack is full");
            }
        else if(rear < size){
            queue[rear] = addNumber;
            //System.out.println("front " + queue[front] + "rear" + queue[rear]);               
            //System.out.println(rear + "add " + queue[rear]);
            count++;
            rear++;
        }
        else{
            rear = 0;
            queue[rear] = addNumber;
            //System.out.println(rear + "add " + queue[rear]);
            //System.out.println("front " + queue[front] + "rear" + queue[rear]);   
            rear++;
            count++;
        }
    return queue; 
    }


    //removes from the queue
    public int[] removeQ(int[] queue){

        if(count == 0){
            System.out.println("stack is empty, cannot remove anything");
        }
        else if(front < (size)){
            //System.out.println("remove:front " + queue[front]+ " count " + count 
            //                  + " front " + front);           
            front++;
            count--;
            //System.out.println("count" + count);
            //System.out.println("remove:front " + queue[front]+ " count " + count);
        }
        else{
            front = 0;
            count--;
            //System.out.println("remove:front " + queue[front]);
            front++;

            //System.out.println("EMPTIED front " + queue[front] + " count " + count);
        }


        return queue;
    }

    public String printQ(int[] queue){
        String myQueue = "";
        int[] tempQ = new int[5];
        int tempCount = 0, tempFront = 0, tempRear = 0; 
        if(count == 0){
            System.out.println("Stack is empty");
        }
        else{

            while(count > 0){

                if(front < (size)){
                //System.out.println(" front " + front + " queue " + queue[front]);
                tempQ[tempRear] = queue[front];
                myQueue += queue[front] + " ";
                front++;
                tempRear++;
                tempCount++;
                count--;
                }
                else{
                    front = 0;
                    //System.out.println(" front " + front + " queue " + queue[front]);
                    tempQ[tempRear] = queue[front];
                    myQueue += queue[front] + " ";
                    tempRear++;
                    tempCount++;
                    front++;
                    count--;
                }
            }//ends while
            while(tempCount > 0){
                if(tempFront < size){
                    //System.out.println(tempQ[tempFront] + " " + tempCount); 
                     tempFront++;
                     tempCount--;
                }
                else{
                    tempFront = 0;
                    tempCount--;

                }
            }
        }//ends if/else
        return myQueue;
    }

    //reports first number in queue
    public int checkTopQ(int[] queue){

        return queue[rear];
    }

    //clears the queue
    public void clearQ(){
        front = 0;
        rear = 0;
        count = 0;  
    }


    //returns whether queue was empty or not
    public boolean epmtyQ(int[] queue){
        boolean empty = false;

        if(count > 0){
            empty = true;
        }
        return empty;
    }


    //tells whether queue is full or not
    public boolean fullQ(int[] queue){
        boolean full = false;

        if(count == size){
            full = true;
        }
        return full;
    }


    //fills a queue
    public int[] fillQ(int[] queue){
        //System.out.println(rear);

        if(count == size){
            System.out.println("Nothing can be added, its full");
        }
        else{
            while(count < size){
                if(rear < size){
                queue[rear] += (count * rear);
                count++;
                rear++;
                }
                else{
                rear = 0;
                queue[rear] += (count * rear);
                rear++;
                count++;
                }
            }
        }
        return queue;
    }

   //search for a number
    public boolean searchQ(int[] queue){
   int[] tempQ = new int[5];
    int tempCount = 0, tempFront = 0, tempRear = 0; 
   boolean found = false;

   if(count == 0){
            System.out.println("Stack is empty");
        }
        else{

            while(count > 0){

                if(front < (size)){
                //System.out.println(" front " + front + " queue " + queue[front]);
                tempQ[tempRear] = queue[front];

                front++;
                tempRear++;
                tempCount++;
                count--;
                }
                else{
                    front = 0;
                    //System.out.println(" front " + front + " queue " + queue[front]);
                    tempQ[tempRear] = queue[front];

                    tempRear++;
                    tempCount++;
                    front++;
                    count--;
                }
            }//ends while
            while(tempCount > 0){
                if(tempFront < size){
                    //System.out.println(tempQ[tempFront] + " " + tempCount); 
                    tempFront++;
                    tempCount--;
                }
                else{
                    tempFront = 0;
                    tempCount--;

                }
            }
        }//ends if/else


   return found;
   }

   public int[] manipulateQ(int[] queue){


   return queue;
   }
}//ends class



package queue;

import java.util.*;

public class QueueNGobernDemo {

    public static void main(String[] args) {

      //soldierQueue[] soldiers = new soldierQueue[40]; 

        int[] queue = new int [20];
        StringTokenizer st = new StringTokenizer("");
        QueueNGobernClass object = new QueueNGobernClass();

        object.addToQ(queue,5);
        object.addToQ(queue,8);

        object.removeQ(queue);
        object.addToQ(queue, 11);
        object.addToQ(queue, 22);
        object.addToQ(queue, 33);
        object.addToQ(queue, 44);
        object.addToQ(queue, 55);

     System.out.println("queue: " + object.printQ(queue));
     object.fillQ(queue);
    }
}
  • 1
    What is the problem? If you cannot use the Queues provided already with java and this is a homework project heres an example, http://algs4.cs.princeton.edu/43mst/Queue.java.html – Ya Wang Nov 10 '15 at 19:32
  • so what did you try and what exactly is th problem ? – Emerson Cod Nov 10 '15 at 19:33
  • Possible duplicate of [Creating an array of objects in Java](http://stackoverflow.com/questions/5364278/creating-an-array-of-objects-in-java) – Perdomoff Nov 10 '15 at 19:35

0 Answers0