Question: In java, create a program that uses the Ackermann's Function The Ackermann's function is of interest because it grows rapidly with respect to the size
In java, create a program that uses the Ackermann's Function
The Ackermann's function is of interest because it grows rapidly with respect to the size of m and n. It is the simplest example of a well-defined total function which is computable but not primitive recursive. This means it cannot be implemented using only for-loops. Notice that forloops (which have a fixed iteration limit) are a special case of while-loops.
Implement the function as a method in Java and trace the histrory of method invocations as follows (e.g., m=1, n=2): Input two intergers separated by a space character (enter "q" to quit): 1 2 Enter method acker: m = 1, n = 2 Enter method acker: m = 1, n = 1 Enter method acker: m = 1, n = 0 Enter method acker: m = 0, n = 1 Leave method acker: acker(0, 1) = 2 Leave method acker: acker(1, 0) = 2 Enter method acker: m = 0, n = 2 Leave method acker: acker(0, 2) = 3 Leave method acker: acker(1, 1) = 3 Enter method acker: m = 0, n = 3 Leave method acker: acker(0, 3) = 4 Leave method acker: acker(1, 2) = 4 Total number of invocations = 6, result = 4
1. Type the program given below in the "AckerFunction.java" file. Notice that the private class variable spaces is used to record how "deep" the current recursive call is. It should not be accessible from outside of the class, so there is no getter/setter defined for this variable.
2. Create another java file called "AckerApp.java", which contains a main method. This main method is responsible for getting input values for m and n, calling the acker method inside the AckerFunction object and print out the result and number of invocations.
3. When you have finished entering the program, compile and run it. Make sure it works correctly (outputs nothing).
4. Make your program fail-safe for user inputs. In other words, when the user input is invalid, the system should not break.
(use with #1):
public class AckerFunction{
private int spaces = 0;
private int numberOfInvocations = 0;
// getter for data field "numberOfInvocations"
public int countOfInvocations(){
return numberOfInvocations;
}
public int acker(int m, int n){
int result = 0;
//TODO: implement the Ackermann's function to trace the method invocation
// history and count the totoal number of invocations.
return result;
}
// Indent the trace messages according to how "deep" the current recursive call is.
// To be called by method acker only.
private void printSpaces(){
for (int i = 0; i < spaces; i++)
System.out.print(" ");
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
