/** * Class for doing recursive maths. Assumes all integers are * non-negative (for simplicity no checks are made). * @author Cara MacNish */ public class RMaths { /** * increment an integer * @param i the integer to increment * @return i + 1 */ public static int increment(int i) {return i + 1;} /** * decrement an integer * @param i the integer to decrement * @return i - 1 */ public static int decrement(int i) {return i - 1;} /** * add two integers * @param x the first integer * @param y the second integer, must be >= 0 * @return x + y */ public static int add(int x, int y) { if (y == 0) return x; else return add(increment(x), decrement(y)); } /** * multiply two integers * @param x the first integer * @param y the second integer, must be >= 0 * @return x × y */ public static int multiply(int x, int y) { if (y == 0) return 0; else return add(x, multiply(x, decrement(y))); } }