CSU Banner

CS 163/164, Summer 2018

Lab 17 - Recursion

Thursday, Jul. 19th

Overview

This lab will introduce and practice with recursion! We will be doing a few tasks, all using recursion - NO LOOPS ALLOWED! You will create an R17 project and class and implement the following interface: IR17.java

Assignment Overview

Recursion is essentially when a method calls itself, asking for the solution of a smaller instance of the problem.
Two things are vital when using recursion: a base case (or base cases) and the recursive case.

The TAs will work with you to implement a made-up number sequence using recursion (pracSeq1). The desciption for this sequence is provided as the postcondition in the interface

Next, complete the code for methods
sequence2
and
sequence3
according to the pre and post-conditions.

Both of these must be recursive and cannot use loops. Think about what the base cases are and what the recursive case is.

Below is test code for you to run and verify with the TA for credit for today's lab.

public static void main(String[] args) {

	R17 rec = new R17();
	System.out.println("pracSeq1(int x):");
	System.out.println("Answer: " + rec.pracSeq1(5) + "   Expecting: 32");
	System.out.println("Answer: " + rec.pracSeq1(7) + "   Expecting: 128\n");

	System.out.println("sequence(int x):");
	System.out.println("Answer: " + rec.sequence2(4) + "   Expecting: 11");
	System.out.println("Answer: " + rec.sequence2(5) + "   Expecting: 20\n");

	System.out.println("sequence3:");
	System.out.println("Answer: " + rec.sequence3(2) + "   Expecting: 7");
	System.out.println("Answer: " + rec.sequence3(3) + "   Expecting: 20");
	System.out.println("Answer: " + rec.sequence3(6) + "   Expecting: 547\n");

}


Show your work to the TA for credit for this lab.

If, after this lab, you feel like you need more recursion practice, you can also complete this lab.


CS Banner
CS Building