Presentation is loading. Please wait.

Presentation is loading. Please wait.

 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

Similar presentations


Presentation on theme: " The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might."— Presentation transcript:

1

2  The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might do with a while loop.

3  A recursive definition is given below for finding how many times the constant value 2 will go evenly into another number.  The value 1 which is added in the recursive case counts how many times recursion occurs.  This count gives the result of the division.  Notice that in the recursive case n is no longer decremented by 1.  It is decremented by 2.

4  The test for the base case is an inequality rather than an equality since the values that n takes on vary depending on whether it is initially odd or even.  Recursive case: n >= 2  f(n) = 1 + f(n – 2)  Base case: n < 2  f(n) = 0

5  Here is a sample implementation.  The recursive method recursiveDivisionByTwo() is typed int and takes an int parameter named dividend.  public static int recursiveDivisionByTwo(int dividend)  {  if(dividend < 2)  return 0;  else  return 1 + recursiveDivisionByTwo(dividend – 2);  }

6  Building on this example, it is possible to write a recursive definition for the general division function.  This is shown on the next slide. The function has two parameters.  Adding 1 in the recursive case again counts how many times the recursion occurs.  In this definition the first parameter for the recursive case is dividend – divisor.  The amount decremented each time is always the same, but divisor can be any (positive integer) value.  The second parameter is the divisor, which is passed down unchanged through all of the calls.

7  Recursive case: dividend >= divisor  f(dividend, divisor) = 1 + f(dividend – divisor, divisor)   Base case: dividend < divisor  f(dividend, divisor) = 0

8  Here is a complete driver program and an implementation of this function.  As usual, this is not a general implementation, but it works for appropriate input values and illustrates the key points.  Notice that a variable retVal is used in the implementation.

9  import java.util.Scanner;   public class GenRecDivProg  {  public static void main(String[] args)  {  Scanner in = new Scanner(System.in);  int retVal;  int dividend, divisor;  System.out.print("Enter the dividend: ");  dividend = in.nextInt();  System.out.print("Enter the divisor: ");  divisor = in.nextInt();  retVal = GenRecDivClass.genRecDiv(dividend, divisor);  System.out.println(retVal);  }

10  public class GenRecDivClass  {  public static int genRecDiv(int dividend, int divisor)  {  int retVal = 0;   if(dividend < divisor)  return retVal;   else  {  retVal = 1 + genRecDiv(dividend - divisor, divisor);  return retVal;  }

11  Just as division can be defined by repeated subtraction, finding a logarithm can be defined by repeated division.  For a given number to find the log of, which will be represented by the variable name toFindLogOf, and a given base, a simple recursive definition of the logarithm function would look like this:  Recursive case: toFindLogOf >= base  f(toFindLogOf, base) = 1 + f(toFindLogOf / base, base)   Base case: toFindLogOf < base  f(toFindLogOf, base) = 0

12  The following example contains two static methods, both of which find a simple logarithm by doing repeated division.  One does so with looping and the other with recursion.  For positive input consisting of a given double toFindLogOf and a given integer base, the code gives as output the largest integer value less than or equal to the logarithm for that number and base.

13  import java.util.Scanner;   public class TestRecAndLoop  {  public static void main(String[] args)  {  Scanner in = new Scanner(System.in);   double toFindLogOf;  int base;  double answer;   System.out.println("What number would you like to find the log of?");  toFindLogOf = in.nextDouble();   System.out.println("What should the base of the logarithm be?");  base = in.nextInt();   answer = RecursionAndLooping.myLogLooping(toFindLogOf, base);   System.out.println("The looping answer is: " + answer);   answer = RecursionAndLooping.myLogRec(toFindLogOf, base);   System.out.println("The recursive answer is: " + answer);  }

14  public class RecursionAndLooping  {  public static int myLogLooping(double toFindLogOf, int base)  {  int retVal = 0;   while(toFindLogOf >= base)  {  toFindLogOf = toFindLogOf / base;  retVal++;  }  return retVal;  }   public static int myLogRec(double toFindLogOf, int base)  {  int retVal = 0;   if(toFindLogOf < base)  return retVal;   else  {  toFindLogOf = toFindLogOf / base;  retVal = 1 + myLogRec(toFindLogOf, base);  return retVal;  }

15  Note that the following version of the recursive method is shorter, but it might also be a little bit harder to understand.   public static int myLogRec(double toFindLogOf, int base)  {  int retVal = 0;  if(toFindLogOf > base)  {  toFindLogOf = toFindLogOf / base;  retVal = 1 + myLogRec(toFindLogOf, base);  }  return retVal;  }


Download ppt " The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might."

Similar presentations


Ads by Google