tags:

views:

44

answers:

2

We are planning to do something similar to what is available in this site AAAMath. We would like to generate sample questions which will add / mult / div / sub (either 2 numbers or 3 numbers or one of the numbers could be a constant ..). Would like to know if there are any libraries which provide a higher level abstraction on the above basic math operations (e.g. provide a single method which is capable of handling addition of either 2 values, 3 values or a [randomized range of inputs] + a constant value which gives you a set of answers).

+1  A: 

Not sure what you are getting at here but java 5 and higher have variadic methods which make this easy to do yourself, e.g.

public static int add(int ...addends)
{
    long sum = 0;
    for(int addend : addends)
    {
        sum += addend;
    }
    if (sum > Integer.MAX_VALUE || sum < Integer.MIN_VALUE)
    {
        throw new RuntimeException("summation has over- or underflowed");
    }
    return (int) sum;
}

public static void main(String[] args)
{
    System.out.println("sum is: " + add(10, 11, 1029102, -10));
}
GregS
+2  A: 

I like the abstractions provided by JScience; but if you need to evaluate arithmetic expressions, this example may serve as a guide.

trashgod