Question

In: Computer Science

There is a Java program that is missing one recursive function: public class Ackermann { /*...

There is a Java program that is missing one recursive function:

public class Ackermann {


  /*            / n+1                     when m = 0
   * ack(m,n) = | ack(m-1,1)              when m > 0 and n = 0
   *            \ ack(m-1, ack(m, n-1))   otherwise
   */
  public static int ack(int m, int n) {
    return 0;
  }

    

  /* Ackermann's Function Test Framework
   *
   * Be extremely careful with these test cases.  Ackermann's grows very fast.
   * For example, ack(4, 0) = 13, but ack(5,0) = 65533.  I doubt the stack is
   * large enough to compute ack(5,0) on the lab machines.  
   */
  public static void main(String[] args) {
    int[] inputM  = { 0,  4,  0,   3};
    int[] inputN  = { 0,  0,  3,   4};
    int[] expect  = { 1, 13,  4, 125};
    boolean error = false;

    assert(inputM.length == inputN.length);

    for(int i = 0 ; i < inputM.length; i++) {
      int answer = ack(inputM[i], inputN[i]);
      if(answer != expect[i]) {
        System.out.printf("ERROR: ack(%d,%d) returned %d not %d.\n",
                          inputM[i], inputN[i], answer, expect[i]);
        error = true;
      }
    }

    if(error)
      System.exit(1);
    else
      System.out.println("Good Job!");
  }
}

Solutions

Expert Solution

public class Ackermann {


    /*            / n+1                     when m = 0
     * ack(m,n) = | ack(m-1,1)              when m > 0 and n = 0
     *            \ ack(m-1, ack(m, n-1))   otherwise
     */
    public static int ack(int m, int n) {
        if (m == 0)
            return n + 1;
        else if (m > 0 && n == 0)
            return ack(m - 1, 1);
        else
            return ack(m - 1, ack(m, n - 1));
    }


    /* Ackermann's Function Test Framework
     *
     * Be extremely careful with these test cases.  Ackermann's grows very fast.
     * For example, ack(4, 0) = 13, but ack(5,0) = 65533.  I doubt the stack is
     * large enough to compute ack(5,0) on the lab machines.
     */
    public static void main(String[] args) {
        int[] inputM = {0, 4, 0, 3};
        int[] inputN = {0, 0, 3, 4};
        int[] expect = {1, 13, 4, 125};
        boolean error = false;

        assert (inputM.length == inputN.length);

        for (int i = 0; i < inputM.length; i++) {
            int answer = ack(inputM[i], inputN[i]);
            if (answer != expect[i]) {
                System.out.printf("ERROR: ack(%d,%d) returned %d not %d.\n",
                        inputM[i], inputN[i], answer, expect[i]);
                error = true;
            }
        }

        if (error)
            System.exit(1);
        else
            System.out.println("Good Job!");
    }
}



Related Solutions

There is a Java program that is missing one recursive function: public class BinarySearch { /*...
There is a Java program that is missing one recursive function: public class BinarySearch { /* / -1 when min > max * | mid when A[mid] = v * search(A, v, min, max) = | search(A,v,mid+1,max) when A[mid] < v * \ search(A,v,min,mid-1) otherwise * where mid = (min+max)/2 */ public static int search_rec(int[] A, int v, int min, int max) { return 0; } public static int search(int[] A, int v) { return search_rec(A, v, 0, A.length-1); }...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static...
The following Java program is NOT designed using class/object concept. public class demo_Program4_non_OOP_design { public static void main(String[] args) { String bottle1_label="Milk"; float bottle1_volume=250; float bottle1_capacity=500; bottle1_volume=addVolume(bottle1_label, bottle1_volume,bottle1_capacity,200); System.out.println("bottle label: " + bottle1_label + ", volume: " + bottle1_volume + ", capacity: " +bottle1_capacity); String bottle2_label="Water"; float bottle2_volume=100; float bottle2_capacity=250; bottle2_volume=addVolume(bottle2_label, bottle2_volume,bottle2_capacity,500); System.out.println("bottle label: " + bottle2_label + ", volume: " + bottle2_volume + ", capacity: " +bottle2_capacity); } public static float addVolume(String label, float bottleVolume, float capacity, float addVolume)...
Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long...
Problem 3: (a) Implement a sublinear running time complexity recursive function in Java public static long exponentiation(long x, int n) to calculate x^n. Note: In your function you can use only the basic arithmetic operators (+, -, *, %, and /). (b) What is the running time complexity of your function? Justify. (c) Give a number of multiplications used by your function to calculate x^63. Important Notes: • For the item (a): o You must add the main method in...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT