Here's the problem that I have to solve:
Write a Java program that uses a Stack data structure to evaluate postfix expressions. Your program takes a postfix expression as an input,for example:3 42.3+ 5.25* ,from the user and calculates/display the result of the expression.
What I'm having trouble is getting it to reading and calculating it as postfix
Here's my code:
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args)
{
Scanner scan = new
Scanner(System.in);
myStack<Double>
stack = new myStack<Double>();
System.out.println("Please enter a postfix expression: ");
String input =
scan.nextLine();
String[] values =
input.split(" ");
for(String terms :
values)
{
if(terms.equals("+") || terms.equals("-") || terms.equals("*") ||
terms.equals("/") || terms.equals("^") )
{
Double var2 = stack.pop();
Double var1 = stack.pop();
switch(terms)
{
case "+":
stack.push(var1 + var2);
break;
case "-":
stack.push(var1 - var2);
break;
case "*":
stack.push(var1 * var2);
break;
case "/":
stack.push(var1 / var2);
break;
//
case "^":
stack.push(var1 * var1);
default:
break;
}
}
}
double value =
Double.parseDouble(values[2]);
System.out.println();
}
}
A separate class called MyStack:
import java.util.Stack;
public class MyStack<T> implements StackInterface<T>
{
Stack<T> mystack;
public MyStack()
{
mystack = new Stack<>();
}
@Override
public void push(T newEntry)
{
mystack.push(newEntry);
}
@Override
public T peek()
{
return
mystack.peek();
}
@Override
public T pop()
{
return
mystack.pop();
}
@Override
public boolean isEmpty()
{
return
mystack.isEmpty();
}
@Override
public void clear()
{
mystack.clear();
}
}
Stack Interface class:
public interface StackInterface<T>
{
//@param
public void push(T
newEntry);
//@return
public T pop();
//@return
public T peek();
//@return
public boolean
isEmpty();
public void
clear();
}
I have changed the code only in Assignment2.java class:
code:
import java.util.ArrayList;
import java.util.Scanner;
public class Assignment2 {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
MyStack<Double> stack = new MyStack<Double>();
System.out.println("Please enter a postfix expression: ");
String input = scan.nextLine();
String[] values = input.split("[ |+|*]");
for(String s:values) {
if(!s.matches("\s*")) {
stack.push(Double.parseDouble(s));
//System.out.println(s);
}
}
ArrayList<String> sign=new ArrayList<String>();
for(int i=0;i<input.length();i++) {
if (input.charAt(i) == '+' || input.charAt(i) == '-'
|| input.charAt(i) == '*' || input.charAt(i) == '/' ||
input.charAt(i) == '^') {
sign.add(String.valueOf(input.charAt(i)));
}
}
for(String terms : sign)
{
if(!terms.matches("\s*")) {
// System.out.println(terms);
if(terms.equals("+") || terms.equals("-") || terms.equals("*") ||
terms.equals("/") || terms.equals("^") )
{
Double var2 = stack.pop();
Double var1 = stack.pop();
switch(terms)
{
case "+":
stack.push(var1 + var2);
break;
case "-":
stack.push(var1 - var2);
break;
case "*":
stack.push(var1 * var2);
break;
case "/":
stack.push(var1 / var2);
break;
//
case "^":
stack.push(var1 * var1);
default:
break;
}
}}
} /*
* double value =
Double.parseDouble(values[2]); System.out.println();
*/
System.out.println(stack.pop());
}
}
output screnshot:

Here's the problem that I have to solve: Write a Java program that uses a Stack...
Convert infix to postfix, and evaluate postfix using custom Stack created using a singly linked list. This is only supposed to use THAT method, calling a normal Stack will give me a zero. I do have the conversion to postfix, but there may be error in there. But the main problem currently is the evaluation of postfix. I keep getting an error that I made for an empty stack, which I will include. For testing it is only supposed to...
Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...
I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> { // Data fields private Node<T> topNode; // references the first node in the chain private int numberOfEntries; public...
java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> { LinkedList<T> list; public Stack() { list = new LinkedList<T>(); } public boolean isEmpty() { return list.isEmpty(); ...
Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException at InfixExpression.execute(InfixExpression.java:98) at InfixExpression.Evaluate(InfixExpression.java:65) at InfixExpression.setWholeExpr(InfixExpression.java:24) at InfixExpression.<init>(InfixExpression.java:17) at Main.testHW1(Main.java:17) at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...
JAVA LANG PLEASE: I have follwed these below guidelines but when i run my queue test it is not executing but my stack is working fine, can you fix it please! MyQueue.java Implement a queue using the MyStack.java implementation as your data structure. In other words, your instance variable to hold the queue items will be a MyStack class. enqueue(String item): inserts item into the queue dequeue(): returns and deletes the first element in the queue isEmpty(): returns true or false...
QUESTION: ADT stack: resizable array-based implementation for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions: bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public: /** Sees whether this stack is empty. @return True if the...
JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings in the stack b. A private data field size to store the number of strings in the stack c. A constructor to construct an empty stack with a default capacity of 4 d. A constructor to construct an empty stack with a specified capacity e. A method empty() that returns true if the stack is empty f. A method push(String value) that stores value...
Im try to create a java program that checks to see if a given boolean expression is a tautology or not my code so far is as follows: public static class TreeNode { char data; TreeNode left; TreeNode right; TreeNode(char item) { data = item; left = null; right = null; } } public static...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...