Question: Write a Java program that replaces add, multiply, divide, and modulo (% in Java) with recursive functions. add() receives two integers, a and b, and
Write a Java program that replaces add, multiply, divide, and modulo (% in Java) with recursive functions. add() receives two integers, a and b, and returns a + b. sum() is recursive, and is allowed to add or subtract one, but not other numbers. Thus, to add 4 + 3, one might add one to three four times. No while, for, or do-while loops are allowed. If either argument is negative, throw an IllegalArgumntException with an appropriate message. mult() receives two integers, a and b, and returns a * b. mult() is recursive, and uses add() for all addition. For example, to multiply 3 * 4, one would add 4 to itself three times. No while, for, or do-while loops are allowed. If either argument is negative, throw an IllegalArgumntException with an appropriate message. div() receives two integers, a and b, and returns their integer quotient a/b. For example, 4/3 = 1, 9/3 = 3, 9/4 = 2. div() is recursive(), and calculates the quotient by repeated subtraction. Subtracting one argument from the other and addition of one are allowed, but no while, for, or do-while loops, and no / operators are allowed. If the numerator is negative, or the denominator is less than or equal to zero, throw an IllegalArgumntException with an appropriate message. mod() receives two integer arguments, a and b, and returns a % b. For example, 4%3 = 1, 9%3 = 0, 9%4 = 1. mod() is recursive(), and calculates the quotient by repeated subtraction. Subtracting one argument from the other is allowed, but no while, for, or do-while loops, and no / or % operators are allowed. If the numerator is negative, or the denominator is less than or equal to zero, throw an IllegalArgumntException with an appropriate message. Each function must be pure, meaning no functions modify any non-local data. Write a test driver (main() or something called by main()) that shows the output that results by adding, subtracting, multiplying, dividing, and calculating the modulo of a and b for the following pairs: 3, 7 3, 0 0, 3 3, 1 1, 3 4, -1 -4, 1 Note that some of the above will cause exceptions to be thrown. These exceptions must be caught and a message for each exception displayed on System.err. For each calculation that does not throw an exception, the test driver should generate output if and only if the value returned is incorrect. For example, add(3,7) should return 10. The result of add(3,7) can be compared to the result of 3+7 to determine if they are the same. If all tests pass, output a message to that effect. If any tests fail, the program should halt immediately after displaying the error message. One way to stop the program is via System.exit(error_code), where error code is any non-zero number. The program must begin with a comment identifying the programmer or pair of programmers and briefly describing what the program does.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
