This is my main method for a stacks/queues assignment. I keep getting an error with my queue, but not my Stack. The stack class seems to be working just fine. I'm completely stuck. It says "cannot instantiate type Queue". Any help would be most appreciated!
public class mainMeth {
public static void main(String[] args) throws FileNotFoundException {
File Polish = new File("fILE4INPUT.txt");
File out = new File("outfile.txt");
Scanner f = new Scanner(Polish);
Queue inputQ = new Queue();
Stack stack2 = new Stack();
Queue outputQ = new Queue();
String word;
Character ch;
while (f.hasNext()) {
String myString = f.nextLine();
for (int count = 0; count < myString.length(); count++) {
ch = myString.charAt(count);
inputQ.addtoRear(ch);
}
while (!inputQ.ismtQ()) {
ch = inputQ.remfront();
if (isAlpha(ch)) {
// System.out.println(ch);
outputQ.addtoRear(ch);
} else {
if (isOperator(ch)) {
if (stack2.ismt()) {
stack2.push(ch);
} else {
if (valueOf(ch) > valueOf(stack2.top())) {
stack2.push(ch);
} else {
outputQ.addtoRear(stack2.pop());
stack2.push(ch);
}
}
}
}
}
while (!stack2.ismt()) {
outputQ.addtoRear(stack2.pop());
}
System.out.println(outputQ.toString() + "\n\n");
while (!outputQ.ismtQ()) {
outputQ.remfront();
}
}
}
public static boolean isAlpha(Character ch) {
boolean retVal = false;
if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
retVal = true;
return (retVal);
}
public static boolean isOperator(Character ch) {
boolean retVal = false;
if (ch == '+' || ch == '-' || ch == '/' || ch == '*')
retVal = true;
return (retVal);
}
public static int valueOf(Character ch) {
int retval = 0;
if (ch == '/' || ch == '*')
retval = 2;
else
retval = 1;
return retval;
}
}
Abstract class, we have heard that abstract class are classes which can have abstract methods and it can't be instantiated. We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.
Stack is the class and can be instantiated directly. This is by design and with Queue , lot more options are there using implementing classes.
For ArrayDeque to function as a queue (FIFO) rather than a stack (LIFO), you should use add and remove . If you use push and pop , it behaves as a stack. (Strictly speaking, remove and pop are the same, but since add/pop or push/remove don't sound good as pairs, we go with add/remove and push/pop .)
In the Interfaces section of the Java docs:
Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
Then you can't directly instantiate the interface Queue<E>
. But, you still can refer to an object that implements the Queue
interface by the type of the interface, like:
// As I saw that you are adding Characters to your queue
Queue<Character> inputQ = new PriorityQueue<Character>();
You can chose the adequate implementation to use regarding your requirements, here is a list of all concrete and known implementing Classes from it's java docs:
ArrayBlockingQueue
,ArrayDeque
,ConcurrentLinkedDeque
,ConcurrentLinkedQueue
,DelayQueue
,LinkedBlockingDeque
,LinkedBlockingQueue
,LinkedList
,LinkedTransferQueue
,PriorityBlockingQueue
,PriorityQueue
,SynchronousQueue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With