Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Consider simple infix expressions that consist of single-digit operands; +,-,*,%, and/; and parentheses. Assume that unary operators are illegal and that the expression contains no

Consider simple infix expressions that consist of single-digit operands; +,-,*,%, and/; and parentheses. Assume that unary operators are illegal and that the expression contains no embedded spaces.

Design and implement a class for the infix calculator. Use the algorithms given in this chapter to convert the infix expression to postfix form and to evalute the resulting postfix expression. Note that if the methodsevaluate and getPostfix method, then the exceptionIllegalStateException should be dirown by these methods.

class Calculator{

public Calculator(String exp)//initializes infix expression

public String toString() // returns infix expression

private boolean convertPostfix() //creates postfix expression

//returns true if successful

//The following methods should throw IllegalStateException if // they are called before convertPostfix

//returns the resulting postfix expression

public String getPostfix() throws IllegalStateException

//evalutes the expression

public int evaluate () throws IllegalStateException //end Calculator

To complete this problem implement a class Stack that implements the Stack ADT from the book which is below.

public class StackReferenceBased implements StackInterface

{

private Node top;

Public StackReferenceBased()

{

top = null;

} //end default constructor

public boolean isEmpty()

{

return top == null;

}//end isEmpty

public void push(Object newItem)

{

top = new Node (newItem, top);

}//end push

public Object pop() throws StackException

{

If (!isEmpty())

{

Node temp = top;

top = top.next;

return temp.Item;

}

else

{

throw new StackException(StackException on + pop: stack empty);

}//end if

}//end pop

public void popAll()

{

top = null;

}//end popAll

public Object peek() throws StackException

{

If(!isEmpty())

{

return top.Item;

}

else

{

throw new StackException (StackException on + peek: stack empty);

}//end if

}//end peek

}//end StackReferenceBased

Implementation on StackInterface

public class StackListBased implements StackInterface

{

private ListInterface list;

public StackListBased()

{

list = new ListReferenceBased();

}//end default constructor

public Boolean isEmpty()

{

return list.isEmpty();

}//end isEmpty

public void push (Object newItem)

{

list.add(0, newItem);

}//end push

public Object pop () throws StackException

{

If (!list.isEmpty())

{

Object temp = list.get(0);

list.remove(0);

return temp;

}

else

{

throw new StackException (StackException on + pop: stack empty);

}//end if

}//end pop

public void popAll()

{

list.removeAll();

}//end popAll

public Object peek() throws StackException

{

If(!isEmpty())

{

return list.get(0);

}

else

{

throw new StackException (StackException on + peek: stack empty);

}//end if

}//end peek

}//end StackListBased

As the problem describes you should create a class Calculator. Your class Calculator should PRINT NOTHING. Bes sure to provide a Unit Test Driver.

An explanation should be given as to which methods are being tested and what the expected results are. The methods should then be tested and the results given showing that the methods works correctly. Use if statements to verify correct results. Be sure to also show and explain examples of when the methods throw exceptions.

BONUS: To receive a 20% bonus make sure your implementation will discard spaces and then recognize multiple adjacent (no spaces between) digits as integers. Processing should follow from there.

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

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions

Question

Which type of soil has more ability to absorb water?

Answered: 1 week ago