How would I go about coding the following Java exercise?
The greatest common divisor of integers x and y is the largest integer that evenly divides into both x and y. Write a recursive method gcd() that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows:
Base Case: if y == 0 then gcd(x, y) is x
General Case: Otherwise gcd(x, y) is gcd(y, x%y)
The gcd method you are writing should include:
public static int gcd(int x, int y)
In the same class, write a main method to test the gcd method. Create a loop to generate ten pseudo-random pairs of values for x and y, where each value is between 5 and 20 inclusive. For each randomly generated pair, display the values for x, y, and the result you get back from gcd(x, y). Use tab characters(" ") to separate the columns of output.
Then, overload the gcd method by including in the same class a method with the signature: public static int gcd(int x, int y, String indent)
This method will use the same recursive algorithm for calculating the greatest common divisor. Inside gcd, before the recursive call, add a line to print the values for which gcd is about to be called. Also, just before returning, gcd prints the value it is about to return, along with the values for which it was called. The indent string is included at the beginning of each of those prints so the output from each call is indented two spaces further to the right. Include indent+" " as an argument for the recursive call to gcd to do this. The indent string should become longer each time gcd is called. In the main method, loop to generate test values for this version of gcd in a similar way as for the first gcd method. Again, loop to generate pseudo-random pairs of values for x and y, where each value is between 5 and 20 inclusive, but this time only loop twice.