Hey guys, I've been having this trouble with the error from my code, debugging doesn't work, but debugs keeps telling me it's because of a ClassNotFoundException which I don't know where to handle it. Anyone know how to fix the error and handle the exception here?
My code:
import junit.framework.TestCase;
import java.util.*;
public class PushPopTest extends TestCase {
public void testExecute() throws Exception {
Stack<Integer> s = new Stack<Integer>();
SymbolTable st = new SymbolTable();
int count = 0;
Push pp = new Push("5");
pp.execute(count,s,st);
assertEquals(1,count); --> AssertionFailedError here ( Expected <1> but was <0>)
assertEquals(5, (int)s.peek());
Pop po = new Pop("x");
po.execute(count, s, st);
assertEquals(2, count);
assertTrue(s.empty());
assertEquals(5, st.getValue("x"));
}
}
import java.util.Stack;
public class Push implements Operation {
String val;
public Push(String value)
{
//this.com = command;
this.val = value;
}
@Override
public int execute(int programCounter, Stack<Integer> stack,
SymbolTable symbolTable) {
if (!(val == null) && !(val.equals("")))
{
if (val.matches("-?\\d+")) //if the value is an integer
{
stack.push(Integer.parseInt(val));
programCounter++;
}
else //if the value is not an integer
{
stack.push(symbolTable.getValue(val)); //use symboltable to check the value
programCounter++;
}
}
else
{
//throw new Exception();
}
return programCounter;
}
}
public class SymbolTable
{
/**
* Creates a SymbolTable.
*/
String na;
int val;
public SymbolTable()
{
//na = "";
//val = 0;
}
/**
* Sets the value of the given variable.
* @param name the name
* @param value the value
*/
public void setValue(String name, int value)
{
na = name;
val = value;
}
/**
* Returns the value of the given variable.
*
* @param name the name
* @return the value
* @throws RuntimeException if the variable is not defined
*/
public int getValue(String name)
{
if (!(na.equals(name)))
{
throw new RuntimeException();
}
return val;
}
}