Using BlueJ to write the code and jUnit to test it out.
Trying to convert a infixToPostfix class I have from my lab class from using char to using strings. This would make it so instead of being limited to single input of say "ab+c-d*-" of char's, it would be able to read "a b + c - d * -"
It's working with a stack which is fairly new to me and I have no idea how exactly I'd go about this. The code I have so far is:
public class InfixToPostfix
{
private Stack operators = new Stack();
/**
* Constructor for objects of class InfixToPostfix
*/
public InfixToPostfix()
{
}
/**
* toPostfix
*/
public String toPostfix(String infix)
{
String [] tokens = new String[100];
int i;
int length = infix.length();
String operator;
String output = "";
for (i = 0; i < length; i++)
{
if (isOperator(tokens[i]))
if (operators.empty())
// 2. If the stack is empty, push the incoming operator onto the stack.
operators.push(tokens[i] + " ");
else
{
if (operatorLessPrecedence(tokens[i]))
// 3. If the incoming symbol has equal or lower precedence than the
// symbol on the top of the stack, pop the stack and print the top
// operator. Then test the incoming operator against the new top of stack.
// Push the incoming symbol onto the stack.
{
do
{
output = output + operators.pop();
}
while (!operators.empty() && operatorLessPrecedence(tokens[i]));
operators.push(tokens[i] + " ");
}
else
// 4. If the incoming symbol has higher precedence than the top of the stack,
// push it on the stack.
operators.push(tokens[i]);
}
else
// 1. Print operands as they arrive.
output = output + tokens[i] + " ";
}
while (!operators.empty())
{
// 5. At the end of the expression, pop and print all operators on the stack.
operator = (String)operators.pop();
output = output + operator + " ";
}
return output;
}
/**
* isOperator
*/
public boolean isOperator(String c)
{
if( c.equals("/") ||
c.equals("'") ||
c.equals("+") ||
c.equals("-"))
return true;
else
return false;
}
/**
* operatorLessPrecedence
* Compare operator with top of stack
* Assume association left to right
*/
public boolean operatorLessPrecedence(String o)
{
int operatorPrecedence = precedence(o);
int tosPrecedence = precedence((String)operators.peek());
return (operatorPrecedence <= tosPrecedence);
}
/**
* precedence
*/
public int precedence(String o)
{
switch (o)
{
case "+": return 1;
case "-": return 1;
case "*": return 2;
case "/": return 2;
}
return 5;
}
}
So when I test in jUnit using a assertEquals;
@Test
public void testAddSub()
{
InfixToPostfix test = new InfixToPostfix();
assertEquals("1 2 +", test.toPostfix("1 + 2"));
assertEquals("2 1 -", test.toPostfix("2 - 1"));
}
I get an exception method currently, before i changed the isOperator method from "==" which was used for testing char's to what I thought was correct, the .equals() method to test strings, I would only get null outputs..
I don't want a straight up code or what exactly I'm doing wrong, just a "forceful" nudge in the right direction or something I can look into. Thanks.
An array of objects holds a reference to the object. When you initialized the array, you just allocated the memory for 100 empty spots. You have to put some real Strings objects on it, otherwise NullPointerException
'll be your output.
So, in your line
String [] tokens = new String[100];
You have an array of 100 Strings references which values are null.
The correct way to compare String
objects is using equals
method.
Using ==
you'll test object references, equals
tests the String
value. So, don't change your method implementation, you are in the right path.
I don't recommend the use of Stack object. As you stated that Stack is a new thing for you and you are trying to improve, I would recommend that you take a look at this discussion if you have time available and get your own conclusion. Why should I use Deque over Stack?.