Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maths operators

I have this method which will generate a random maths expression solve it and output the answer to a variable:

public int Nov2()
{
    char[] ops = new char[] {'+', '-', '*', '/'};
    int i = rand.nextInt(4-0) + 0;
    char op1 = ops[i];

    int novnum1 = rand.nextInt(101-1) + 1;

    int novnum2 = rand.nextInt(101-1) + 1;

    int nov2result = 0;

    switch(op1) {
        case '+': nov2result = novnum1 + novnum2; break;
        case '-': nov2result = novnum1 - novnum2; break;
        case '*': nov2result = novnum1 * novnum2; break;
        case '/': nov2result = novnum1 / novnum2; break;
    }

    String nov2Exp = novnum1 + " " + op1 + " " + novnum2 + " = ";

    Nov2resstor = nov2result;

    setContentView(R.layout.gameview);

    TextView display = (TextView) findViewById(R.id.exp);

    display.setText(nov2Exp);

    return nov2result;
}

How would i use the same sort of thing for expressions with more than two terms without having to write really complex if statements like this in my next method:

public int Eas3()
{
    char[] ops = new char[] {'+', '-', '*', '/'};
    int i = rand.nextInt(4-0) + 0;
    char op1 = ops[i];
    i = rand.nextInt(4-0) + 0;
    char op2 = ops[i];

    int easnum1 = rand.nextInt(101-1) + 1;

    int easnum2 = rand.nextInt(101-1) + 1;

    int easnum3 = rand.nextInt(101-1) + 1;

    int eas3result = 0;


    if (op1 == '+' && op2 == '+')
    {
        eas3result = ((easnum1 + easnum2) + easnum3);
    }
    else if (op1 == '+' && op2 == '-')
    {
        eas3result = ((easnum1 + easnum2) - easnum3);
    }
    else if (op1 == '+' && op2 == '*')
    {
        eas3result = ((easnum1 + easnum2) * easnum3);
    }
    else if (op1 == '+' && op2 == '-')
    {
        eas3result = ((easnum1 + easnum2) - easnum3);
    } 
.../

I have methods which do this for 2,3,4,5 and 6 so my if statements would become very large using this method.

Any ideas?

like image 596
nexus490 Avatar asked Mar 11 '26 23:03

nexus490


2 Answers

you can use the built-in Javascript engine.

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;

public class Test 
{
  public static void main(String[] args) throws Exception
  {
       ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");
        String foo = "40+2";
        System.out.println(engine.eval(foo));
    } 
}
like image 155
AndreiBogdan Avatar answered Mar 14 '26 12:03

AndreiBogdan


Yes, another way to do it is to write Command objects:

public interface Command<V> {
    V execute(Object ... args); 
}

You'll write an object that implements this interface:

public class AdditionCommand implements Command<Double> {
    public Double execute(Object ... args) {
        Double x = (Double)args[0];
        Double y = (Double)args[1];
        return x+y;    
    }
}

Now you can look up in a Map using the operator:

Map<String, Command> opsLookup = new HashMap<String, Command>() {{
   opsLookup.put("+", new AddCommand<Number>());
   opsLookup.put("-", new SubtractCommand<Number>());
}};

No need for a switch.

like image 25
duffymo Avatar answered Mar 14 '26 13:03

duffymo