Question: Take the following java code (2 classes) and fix it. In the AckerFunction class, implement the acker function and make sure the program prints out
Take the following java code (2 classes) and fix it. In the AckerFunction class, implement the acker function and make sure the program prints out the Enter Method Acker as well as the Leave Method Acker as shown below

In the AckerApp Class, make sure to keep the program running even after it givs out an input, only allowing the user to quit by pressing 'q'. Also make sure the program doesnt fail if the user inputs an invalid string ( only positive whole numbers can be accepted)
import java.util.Scanner;
public class AckerApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
//enter value of m and n
System.out.println("Enter the value of m and n:");
// prevents invalid string input
while(!scanner.hasNextInt()) {
scanner.next();
System.out.println("Enter the value of m and n:");
}
// get input from user
int m = scanner.nextInt();
int n = scanner.nextInt();
// prevents negative integer from returning function, if no negative then it returns function
if (m > 0 && n > 0 ) {
AckerFunction af = new AckerFunction();
int result = af.acker(m, n);
System.out.println("Number of Invocations: " + af.countOfInvocations() + ", result = " + result);}
else {
System.out.println("Input a positive integer (press 'q' to quit");
}
}
}
public class AckerFunction {
// int for spaces and invocations ( kept private)
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;
numberOfInvocations++;
//TODO: implement the Ackermann's function to trace the method invocation
// history and count the total number of invocations
if(m==0){
result = n+1;
}else if(m > 0 && n == 0) {
spaces++;
result = acker(m-1,1);
spaces--;
}else if (m > 0 && n > 0) {
spaces++;
result = acker(m-1, acker(m, n-1));
spaces--;
}
printSpaces();
System.out.println("Leave method acker(" + m + "," + n + ")= " + result);
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
System.out.print(" ");
}
}
ifm=0 If n=0 otherwise 2+1 Acker (m,n)-Acker (m-1,1) Acker (m-1,Acker(m,n 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) 2 Enter method acker: m = 1, n = 2 Enter method acker: m= 1, n Enter method acker: m = 1, n Enter method acker: m = 0, n- Leave method acker: acker (0, 1) Leave method acker: acker (1, 0) Enter method acker: m = 0, n = 2 Leave method acker: acker (0, 2) Leave method acker: acker (1, 1) Enter method acker: m = 0, n Leave method acker: acker (0, 3) Leave method acker: acker (1, 2) = 4 Total number of invocations = 6, result
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
