I am trying to add x after the answer, I have tried the way, but one method can only have one return.
the first if is to remove the x from string, after the divide, I want to add it back.
public String apply(Vector args)
{
//define two string variables
String expString1 = (String)args.get(0);
String expString2 = (String)args.get(1);
String s = "";
//move the x if the last char is x.
if (expString1.charAt(expString1.length()-1) == 'x'){s = expString1.substring(1, expString1.length()-1);
}else if (expString1.charAt(expString1.length()-1) != 'x') {s = expString1;}
//convert string to int, and do the divide operator.
int n2 = Integer.parseInt(expString2);
int n1 = Integer.parseInt(s);
int result = n1 / n2;
//get result, but not the one I want, especially for x string.
return String.valueOf(result);
}
this is what I want get.
public void testApplyVector() {
Vector arg = new Vector();
arg.add("+6x");
arg.add("2");
Divide add = new Divide();
assertEquals("+3x", add.apply(arg));
Vector arg2 = new Vector();
arg2.add("12");
arg2.add("2");
assertEquals("6", add.apply(arg2));
}
but this is the JUnit get, the first condition can't get the answer. I don't how to add a "x" after the answer.
public void testApplyVector() {
Vector arg = new Vector();
arg.add("+6x");
arg.add("2");
Divide add = new Divide();
assertEquals("3", add.apply(arg));
Vector arg2 = new Vector();
arg2.add("12");
arg2.add("2");
assertEquals("6", add.apply(arg2));
}