2nd part of a homework project which we have strict instructions. In the first part, I have another class which builds an Array of Country objects, which I use to create a Stack. Then the Stack values are supposed to be inserted into the priority Que and sorted alphebetically. Either my stack is not passing the objects correctly, or for whatever reason the priority Que is not receiving them correctly. When I print the array in pQue1 and pQue 2 it returns
null
thanks for any help!
I am very new to java and programming, but from past experiences I feel like the problem lies somewhere in creating the arrays, or somehow I have programmed the methods to pass the array to a completely different instance of priorityQueue??
import java.util.Arrays; public class Stack { private int maxSize; // size of stack array private Countries[] stackArray; // recall: ‘instance variables’ private int top; // top of stack private PriorityQueue pQue1; private PriorityQueue pQue2; public Stack(int s) { maxSize = s; stackArray = new Countries[maxSize]; top = -1; pQue1 = new PriorityQueue(maxSize); pQue2 = new PriorityQueue(maxSize); } // end constructor public void push(Countries j) { // put item on top of stack stackArray[++top] = j; System.out.println(stackArray[top]); if (stackArray[top].getRegionNumber() == 1 ) buildPQue1(stackArray[top]); pQue1.insert(stackArray[top]); if (stackArray[top].getRegionNumber() == 2 ) buildPQue2(stackArray[top]); } // end push public void buildPQue1(Countries k) { pQue1.insert(k); } // end build PQue1() public void buildPQue2(Countries i) { pQue1.insert(i); } // end build P Que 2 // end push() public class PriorityQueue extends Queue { // array in sorted order, from max at 0 to min at nitems-1 private int maxSize; private Countries[] queArray; private Countries[] item; private int nItems; public PriorityQueue(int s) { // constructor super(s); maxSize = s; queArray = new Countries[maxSize]; item = new Countries[maxSize]; }//end constructor @Override public void insert(Countries item) { int j; if (nItems==0) { queArray[nItems++] = item; // System.out.println(queArray[nItems]); }// end if else { for (j=nItems-1; j>=0; j--) { if (item.getName().compareTo(queArray[j].getName()) < 0 ) queArray[j+1] = queArray[j]; else break; } // end for if ( j < maxSize) { queArray[j+1] = item; } // end if // System.out.print(queArray[j]); nItems++; } // end else (nItems > 0) } // end insert() public Countries peekMin() // peek at minimum item { return queArray[nItems-1]; } // end peek min public void displayQue() { System.out.println(queArray[nItems]); } // end display que } // end class PriorityQ