I need help finishing my postFix part of this I don\'t really know what to do. T
ID: 3624644 • Letter: I
Question
I need help finishing my postFix part of this I don't really know what to do. The instructions are underneath here, followed by the code. I'm not positive that my stack class is completely correct but it should be close. Thanks for the help.1. Implement a Stack class with the following methods
1. Internal variables
1. elements
1. An integer array that holds the values in the stack
2. currentSize
1. An integer which holds the current size of the stack
3. maxSize
1. An integer which hold the maximum size of the stack
2. Constructor
4. Takes in the maximum size of the stack, setting the internal array size to be the value given
3. isEmpty
1. No parameters, returns true if the stack is empty, false if it is not
4. isFull
1. No parameters, returns true if the stack is full, false if it is not
5. size
1. No parameters, returns the current number of items in the stack
6. push
1. Takes in an integer, and adds that to the end of the internal array
7. pop
1. No parameters, returns the element at the end of the internal array, and decrements the size of the array
2. Implement a PostFix parser
8. Example post fix expressions
1. 2 3 * = 2 * 3 = 6
2. 2 3 4 * - = 2 - 3 * 4 = -10
3. 1 2 3 4 * * + = 1 + 2 * 3 * 4 = 25
9. Read in the expression from the user, and assume the user will only ever put in expressions with values from 0 - 9, and will never have more than 20 elements in the expression.
10. Process the expression
1. Each time you encounter an integer in the expression, place it in the stack
2. Each time you encounter an operator (+ - * or /), pop the first two elements off the stack, and apply the operator to those two numbers.
1. The first element you pop off should go to the right of the operator, and the second element should go to the left of the operator.
2. Push the result back on the stack.
3. Once you reach the end of the expression, output the result
4. If at any time you try to pop something from the stack, and there is nothing there, an error should be displayed, and execution should terminate.
___________________________Stack Class______________________________________
public class Stack
{
private int [] elements;
private int currentSize;
private int maxSize;
public Stack (int maxSize)
{
int [] elements = new int [maxSize];
this.elements = elements;
this.currentSize=0;
this.maxSize=maxSize;
}
public Boolean isEmpty()
{
Boolean empty = false;
if (currentSize == 0)
empty = true;
return empty;
}
public Boolean isFull()
{
Boolean full = false;
if (currentSize == maxSize)
full = true;
return full;
}
public int size()
{
return this.currentSize;
}
public void push (int number)
{
if (!this.isFull())
{
this.elements[this.maxSize-this.currentSize-1] = number;
this.currentSize++;
}
else
System.out.print("The stack is full. ");
}
public int pop ()
{
int number=0;
if (!this.isEmpty())
{
number = this.elements[this.maxSize-this.currentSize];
this.elements[this.maxSize-this.currentSize]=0;
this.currentSize--;
return number;
}
else
System.out.print("The stack is empty. ");
return number;
}
}
_______________________________PostFix_________________________
import java.util.Scanner;
public class postFix
{
public static int PostFix()
{
Stack s = new Stack(20);
System.out.println("Enter expression ");
Scanner ins = new Scanner(System.in);
int answer =0;
int var1 =0;
int var2 =0;
while (ins.hasNext())
{
if(ins.hasNext()) charAt(0) == '*')
{
var1 = s.pop();
var2 = s.pop();
answer = var1*var2;
s.push(answer);
}
}
}
}
Explanation / Answer
Hey man I see what you're saying. Scanner has some very helpful methods that I believe will resolve what you are having trouble with. Here is what I would do. Import java.util.Scanner; Import java.io.*; public class postFix { // You must have the throws IOException for scanner to work. It has to do with how the console and scanner communicate. public static int PostFix() throws IOException; { Stack s = new Stack(20); System.out.println("Enter expression "); Scanner ins = new Scanner(System.in); int answer =0; int var1 =0; int var2=0; while(ins.hasNext()) { if(ins.hasNextInt()) s.push(ins.nextInt()); else { String op = ins.next(); var1 = s.pop(); var2 = s.pop(); if(op.equals("+")) s.push(var2+var1); else if(op.equals("-")) s.push(var2-var1); else if(op.equals("/")) s.push(var2/var1); else s.push(var2*var1); } } While(!s.isEmpty()); System.out.print(s.pop()+ " ");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.