CS 171 & 171L Introduction to Computer Science I & Lab Fall 2017 

Lab 12

In this lab, you will write a few recursive methods.

1. Introductions

Introduce yourself to your lab partner. Tell them about the strangest food you have ever had. Was it good? Would you eat it again?

2. Recursive Methods

Complete the methods in Lab12.java, as shown below. After you complete each method, demonstrate it to your instructor. Change the driver/navigator roles after each method. Most of these methods will be very simple, but you will need to think carefully about how to define the problem recursively.

// Put your names here

// Complete each of the following methods to compute the
// values as indicated using a recursive algorithm
// Do not use a loop unless specifically indicated


public class Lab12 {
  
  // compute n!
  // Example:
  //   input:  4
  //   output: 24
  public static int factorial(int n) {
    if (n==0) {
       return 1;
       }
    else {
       return n*factorial(n-1);
       }
  }
  
  
  // Sum the first n positive integers
  // Example:
  //   input:  10
  //   output: 55
  public static int sum(int n) {
    return 0;
  }
  

  // Discuss this with your lab partner
  public static int upAndDown(int n) {
    System.out.println("Down: " + n);
    if (n > 0) upAndDown(n-1);
    System.out.println("Up: " + n);
    return 0;
  }


 // Compute the ith Fibonacci number
 // F(0) = 1
 // F(1) = 1
 // F(2) = 2
 // ...
 // F(n) = F(n-1) + F(n)
 // Note: Computing Fibonacci numbers this way is
 //       completely impractical as you will see
 //       Discuss with your lab partner
 public static long fibo(int n) {
   return 0;
 }


  // Return the number of spaces in a string
  // Example:
  //   input:  "This is a test."
  //   output: 3
  // Hint: Use substring and index methods of String
  public static int countSpaces(String s) {
    return 0;
  }


  // Output the number of digits in a given number
  // Example:
  //   input:  0
  //   output: 1
  //   input:  12345
  //   output  5
  // Hint: Use / operator
  public static int digits(int n) {
    return 0;
  }


  // Convert the integer value to a String representing its binary value
  // Example:
  //   input:  12022008
  //   output: 101101110111000011111000
  //   input:  10
  //   output  1010
  // Hint: Use / and % operators
  // Note: 10 is even, so rightmost bit is 0
  //       divide 10 by 2, resulting in 5
  //       5 is odd, so next bit is 1
  //       divide 5 by 2, resulting in 2
  //       2 is even, so next bit is 0
  //       divide 2 by 2, resulting in 1
  //       1 is odd, so next bit is 1
  public static String binary(int n) {
    return "";
  }


  // Multiply the input values using only addition and no loops.
  // Example:
  //   input:  5, 7
  //   output: 35
  public static int multiply(int m, int n) {
    return 0;
  }


  // Return minimal element in array a, starting at position p)
  // Example:
  //   input:  {1,23,54,653,12,-2,4}
  //   output: -2
  //   input:  {12345}
  //   output: 12345
  // Hint: 
  public static int min(int[ ] a, int p) {
    return 0;
  }


  // Reverses String s
  // Example:
  //   input:  "pi"
  //   output: "ip"
  //   input:  "computer"
  //   output: "retupmoc"
  //   input:  "oozy rat in a sanitary zoo"
  //   output: "ooz yratinas a ni tar yzoo"
  // Hint: Reverse what you have been doing
  public static String reverse(String s) {
    return "";
  }


  // Ackermann's Function
  // A(m,n) = n+1 if m=0
  // A(m,n) = A(m-1,1) if n=0
  // A(m,n) = A(m-1,A(m,n-1)) otherwise
  // Note: This is a really C-R-A-Z-Y function!
  //       See http://mathworld.wolfram.com/AckermannFunction.html
  public static int Ackermann(int m, int n) {
    return 0;
  }

  // Determine all the permutations of a given input string
  // Hint: Use a for loop and the substring method.
  // Example:
  //   input:  "abc"
  //   output: ["abc", "acb", "bac", "bca", "cab", "cba"]
  // Note: You will need a for loop and an array
  // Note: There are n! permutations of n elements.
  // Generally recursive, but you can additionally use one for loop
  public static String[] permutations(String s) {
     String[] a; a = null;
     return a;
     }


  public static void main(String args[]) {
    
     System.out.println("sum: " + factorial(4) + " should equal 24");

     System.out.println("sum: " + sum(10) + " should equal 55");

     System.out.println("Why does this print the numbers forward and backward?\n");
     upAndDown(5);

     System.out.println("Fibo "+10+": "+fibo(10));

     System.out.println("countSpaces: " + countSpaces("This is a test.") 
                          + " should equal 3");

     System.out.println("digits: " + digits(12345) + " should equal 5");

     System.out.println("binary: " + binary(5) + " should equal 101");

     System.out.println("multiply: " + multiply(5, 7) + " should equal 35");

     System.out.println("min: " + min(new int [ ] {1,23,54,653,12,-2,4}, 0) 
                          + " should equal -2");

     System.out.println("reverse: " + reverse("computer") + " should be retupmoc");

     System.out.println("Ackermann: " + Ackermann(3,3) + " should equal 61");
     // Why does Ackermann(5,5) cause a stackoverflow error?
     // Discuss this with your lab partner.
     //System.out.println(Ackermann(5,5));     

     System.out.println("\nOnly for the brave!");
     System.out.println("All 24 permutations of abcd: abcd, abdc, acbd, acdb, adbc, adcb, ..., dcba");
     String [] perms = permutations("abcd");
     if (perms != null) {
       for (int i = 0;  i < perms.length;  i++) {
         System.out.print(perms[i]);
         if (i < perms.length-1) System.out.print(", ");
         else System.out.println();
       }
     }

  }

}

You are done!

Finally, demonstrate your program to your instructor or lab assistant and remember to email a copy of the code to your lab partner.

Congratulations!


Copyright © 2017, David A. Reimann. All rights reserved.