Question

In: Computer Science

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

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

public class GCD {

/* There is a neat trick with recursive programming. The function described

   * below requires that y is at most x. Rather than add this to the recursive

   * function definition, we can add a front-end, helper function.

   *

   * I wrote this function for you and I called it gcd. The recursive logic goes

   * in a function called gcd_rec. All gcd does is make sure that x is not

   * smaller than y.    

   */

/* Requires x >= y

   *            / x            when y is 0

   * gcd(x,y) = |

   *            \ gcd(y, x%y) otherwise

   */

public static int gcd_rec(int x, int y) {

    return 0;

}

public static int gcd(int x, int y) {

    if(x < y)

      return gcd_rec(y,x);

    else

      return gcd_rec(x,y);

}

/* Greatest Common Divisor Test Framework

   */

public static void main(String[] args) {

    int[] inputX = {10, 35, 14, 4181};

    int[] inputY = { 8, 15, 35, 6765};

    int[] expect = { 2, 5, 7,    1};

    boolean error = false;

    assert(inputY.length == inputX.length);

    for(int i = 0 ; i < inputX.length; i++) {

      int answer = gcd(inputX[i], inputY[i]);

      if(answer != expect[i]) {

        System.out.printf("ERROR: gcd(%d,%d) returned %d not %d.\n",

                          inputX[i], inputY[i], answer, expect[i]);

        error = true;

      }

    }

    if(error)

      System.exit(1);

    else

      System.out.println("Good Job!");

}

}

Solutions

Expert Solution

The recursive function gcd_rec can be written as follows.  The inline comments are provided for the better understanding of the program logic.

    public static int gcd_rec(int x, int y) {
                if(y == 0)
                        // Return x if y = 0. This is the exit condition of the recursive function.
                        return x;
                else
                        //If not, call the gcd_rec recursively, by passsing in the parameters y, x %y.
                        //x % y is the remainder obtained after dividing x by y.
                        return gcd_rec(y, x % y);
        }

The full program is given below.  

public class GCD {
        /* There is a neat trick with recursive programming. The function described
           * below requires that y is at most x. Rather than add this to the recursive
           * function definition, we can add a front-end, helper function.
           *
           * I wrote this function for you and I called it gcd. The recursive logic goes
           * in a function called gcd_rec. All gcd does is make sure that x is not
           * smaller than y.    
           */
        /* Requires x >= y
           *            / x            when y is 0
           * gcd(x,y) = |
           *            \ gcd(y, x%y) otherwise
           */
        public static int gcd_rec(int x, int y) {
                if(y == 0)
                        // Return x if y = 0. This is the exit condition of the recursive function.
                        return x;
                else
                        //If not, call the gcd_rec recursively, by passsing in the parameters y, x %y.
                        //x % y is the remainder obtained after dividing x by y.
                        return gcd_rec(y, x % y);
        }
        public static int gcd(int x, int y) {
                if(x < y)
                  return gcd_rec(y,x);
                else
                  return gcd_rec(x,y);
        }
        /* Greatest Common Divisor Test Framework
           */
        public static void main(String[] args) {
                int[] inputX = {10, 35, 14, 4181};
                int[] inputY = { 8, 15, 35, 6765};
                int[] expect = { 2, 5, 7,    1};
                boolean error = false;
                assert(inputY.length == inputX.length);
                for(int i = 0 ; i < inputX.length; i++) {
                  int answer = gcd(inputX[i], inputY[i]);
                  if(answer != expect[i]) {
                        System.out.printf("ERROR: gcd(%d,%d) returned %d not %d.\n",
                                                          inputX[i], inputY[i], answer, expect[i]);
                        error = true;
                  }
                }
                if(error)
                  System.exit(1);
                else
                  System.out.println("Good Job!");
        }
}

The screenshots of the program and the output is provided.


Related Solutions

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)...
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); }...
There is a Java program that is missing one recursive function: public class Factorial { /*...
There is a Java program that is missing one recursive function: public class Factorial { /* / 1 when n is 0; * fact(n) = | * \ n*fact(n-1) otherwise */ public static int fact(int n) { return 0; } /* Factorial Test Framework * * Notice the odd expected value for fact(20). It is negative because * fact(20) should be 2432902008176640000, but the maximum int is only * 2147483647. What does Java do when integers run out of range?...
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...
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[],...
Problem 2 Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search...
PROGRAM SIMULATION. Understand the given JAVA program and write the output. 1.     public class Places        {...
PROGRAM SIMULATION. Understand the given JAVA program and write the output. 1.     public class Places        {            public static void main(String args[])            {                   String place[]=new String[4];           place[0]="Salmaniya"; place[1]="Salmabad"; place[2]="Isa Town"; place[3] = “Manama”         System.out.println(place[3]);         System.out.println(place[1].toLowerCase());         System.out.println(place[2].substring(4,6);         System.out.println(place[3].charAt(4));         System.out.println(place[1].equals(place[2]));            } }    b. public class ChangeIt { public void doIt( int[] z ) { z[0] = 0; } } public class TestIt { public static void main ( String[] args ) {...
Using python pls!!!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a...
Using python pls!!!!! Write a recursive function gcd(m,n) that returns the greatest common divisor of a pair of numbers. The gcd of m and n is the largest number that divides both m and n. If one of the numbers is 0, then the gcd is the other number. If m is greater than or equal to n, then the gcd of m and n is the same as the gcd of n and m-n. If n is greater than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT