Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions

Question

Explain the importance of nonverbal messages.

Answered: 1 week ago

Question

Describe the advantages of effective listening.

Answered: 1 week ago

Question

Prepare an employment application.

Answered: 1 week ago