Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 { ListElement head;

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(val, head.getLink())); }

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 where = head.getLink(); while (where != null) { retval += where.getValue() + " "; where = where.getLink(); } return retval; }

public static void main(String args[]) throws StackException { Stack is = new Stack(); for (int i = 0; i < 10; i++) { is.push(i); }

System.out.println(is);

while (!is.isEmpty() ) { System.out.println(is.pop()); } } }

class ListElement { private R value; private ListElement link;

public ListElement(R val, ListElement link) { this.value = val; this.link = link; }

public ListElement(R val) { this(val, null); }

public ListElement() { this(null, null); }

public R getValue() { return this.value; }

public ListElement getLink() { return this.link; }

public void setLink(ListElement newLink) { this.link = newLink; } }

Linked List code if needed too

public class LL { ListElement head, tail; int size;

public LL() { this.head = new ListElement(); this.tail = head; this.size = 0; }

public boolean isEmpty() { return this.size == 0; }

public void insertAtHead(T p) { this.head.setLink(new ListElement(p, head.getLink())); this.size++; if (this.tail == this.head) this.tail = head.getLink(); } public T removeAtHead() throws LLException { T retval = null; if (this.isEmpty()) throw new LLException("Empty List"); else { retval = this.head.getLink().getValue(); this.head.setLink(this.head.getLink().getLink()); if (this.head.getLink() == null) this.tail = this.head; size--; } return retval; }

public void insertAtTail(T p) { tail.setLink(new ListElement(p, tail.getLink())); size++; tail = tail.getLink(); }

public int getSize() { return this.size; }

/*

{ String retval = ""; if (this.isEmpty()) retval = "Empty List"; else { ListElement where = head.getLink(); while (where != null) { retval += where.getValue() + " "; where = where.getLink(); } } return retval; } */ public String toString() { return toString(head.getLink()); }

private String toString(ListElement h) { if(h.getLink() == null) return ""; else { return h.getValue() + " " + toString(h.getLink()); } }

public static void main(String args[]) { LL myLL = new LL(); for(double x = 0.0; x <= 10.0; x += 1.0) { myLL.insertAtTail(x); } System.out.println(myLL); }

public class ListElement { private R value; private ListElement link; public ListElement(R v, ListElement le) { this.value = v; this.link = le; } public ListElement(R v) { this(v, null); } public ListElement() { this(null, null); } public void setValue(R p) { this.value = p; } public R getValue() { return this.value; } public ListElement getLink() { return this.link; } public void setLink(ListElement lnk) { this.link = lnk; }

public String toString() { return " ListElement: value = " + this.value + " link = " + this.link + " "; } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

2. (1 point) Given AABC, tan A b b

Answered: 1 week ago