In case you want to do everything from scratch you ll need to come up with an equation parser.
From there you use an Enum :
enum Operation{ADD, SUBTRACT;
public int evaluate(int operand1, int operand2) throws IllegalOperationException {
switch(this) {
case ADD: return operand1 + operand2;
case SUBTRACT: return operand1 - operand2;
default:
break;
}
throw new IllegalOperationException();
}
public static Operation getOperator(String operator) throws IllegalOperationException{
for(Operation o: Operation.values()){
if(o.toString().equals(operator)){
return o;
}
}
throw new IllegalOperationException();
}
};
So, parse your equation using a stack/queue and then for each operator(op) basically do:
Operation.getOperator(op).evaluate(r1, r2);
OR
Replace x and y with x[i] and y[i] and pass the constructed string
to built in javascript engine, in case you are using jdk1.6 or higher.
ScriptEngineManager sm = new ScriptEngineManager();
ScriptEngine en = sm.getEngineByName("JavaScript");
String expression = //your expression with values;
System.out.println(engine.eval(expression));