Question
I need help writing a main JAVA program and command line to evaluate a postfix expression entered by the user!!!. The lab information and Stack
I need help writing a main JAVA program and command line to evaluate a postfix expression entered by the user!!!. The lab information and Stack code is given below. LAB 1 Using the Stack class I wrote, write a main program that will accept a string of operands (doubles) and operators (+ - / x) on the command line. It should interpret these as reverse Polish (more properly reverse Lukasiewicz) notation, evaluate the expression, and print the final value. Be sure to deal with invalid input in a nice tidy fashion. You can get the individual tokens from the command line using the args array from the public static void main(String args[]) line. You must use x rather than * for multiplication since * has a particular meaning to the Linux command line. As discussed in class, to evaluate a postfix expression, when you see an operand, push it on to a stack. When you see an operator, pop the two top elements from the stack, combine them using the specified operation, and push the result on the stack. When the input stream is exhausted, print the top item on the stack. 4 5 x means 4*5, and should produce 20 1 2 + 3 x is equivalent to the infix expression (1+2)*3 and should yield 9. Things that can go wrong that you need to deal with "nicely." Seeing an operator when there are fewer than two numbers on the stack. Having more than one thing on the stack when the end of args[] is reached. Division by 0. Undefined "operators," for instance 1 3 $ The easiest way to determing whether a token is an operator or an operand is to try to convert it to a double, and catch the NumberFormatException that results if it is not a number. Something like this: double value = 0.0; boolean isOperator = false; String operator = ""; try { value = Double.parseDouble(args[i]); isOperator = false; } catch (NumberFormatException e) { isOperator = true; operator = args[i]; } After this fragment of code, if isOperator is true, opertor should contain + - / or x. If isOperator is false, value will contain the numeric value of the operand.
STACK CODE
public class Stack
public Stack() { head = new ListElement
public boolean isEmpty() { return head.getLink() == null; }
public boolean isFull() { return false; }
public void push(T val) { head.setLink(new ListElement
public T pop() throws StackException { T retval = null; if (this.isEmpty() ) { throw new StackException("Stack Underflow"); } else { retval = head.getLink().getValue(); head.setLink(head.getLink().getLink()); } return retval; }
public String toString() { String retval = ""; ListElement
public static void main(String args[]) throws StackException { Stack
System.out.println(is);
while (!is.isEmpty() ) { System.out.println(is.pop()); } } }
class ListElement
public ListElement(R val, ListElement
public ListElement(R val) { this(val, null); }
public ListElement() { this(null, null); }
public R getValue() { return this.value; }
public ListElement
public void setLink(ListElement
Linked List code if needed too
public class LL
public LL() { this.head = new ListElement
public boolean isEmpty() { return this.size == 0; }
public void insertAtHead(T p) { this.head.setLink(new ListElement
public void insertAtTail(T p) { tail.setLink(new ListElement
public int getSize() { return this.size; }
/*
{ String retval = ""; if (this.isEmpty()) retval = "Empty List"; else { ListElement
private String toString(ListElement h) { if(h.getLink() == null) return ""; else { return h.getValue() + " " + toString(h.getLink()); } }
public static void main(String args[]) { LL
public class ListElement
public String toString() { return " ListElement: value = " + this.value + " link = " + this.link + " "; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started