CSU Banner

CS 163/164, Spring 2018

Lab 19 - Recursion

(Thursday, Apr. 19th || Friday, Apr. 20th)

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 R19 project and class and implement the following interface: IR19.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.

Complete the code for the defined methods according to the pre and post-conditions.

All 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) {

		R19 rec = new R19();
		System.out.println("sumDigits(int x):");
		System.out.println("Answer (1234): " + rec.sumDigits(1234) + "   Expecting: 10");
		System.out.println("Answer (7): " + rec.sumDigits (7) + "   Expecting: 7");
		System.out.println("Answer (6789): " + rec.sumDigits (6789));
		System.out.println();

		System.out.println("countCannonballs (int x):");
		System.out.println("Answer (1): " + rec.countCannonballs(1) + "   Expecting: 1");
		System.out.println("Answer: " + rec.countCannonballs (4) + "   Expecting: 30");
		System.out.println("Answer (10): " + rec.countCannonballs (10));
		System.out.println();
		
		System.out.println("numDigits(int x):");
		System.out.println("Answer (1234): " + rec.numDigits(1234) + "   Expecting: 4");
		System.out.println("Answer (7): " + rec.numDigits (7) + "   Expecting: 1");
		System.out.println("Answer (678900): " + rec.numDigits (678900));
		System.out.println();

		System.out.println("backString(String s):");
		System.out.println("Answer (yes): " + rec.backString("yes"));
		System.out.println("Answer (): " + rec.backString(""));
		System.out.println("Answer (a): " + rec.backString("a"));
		System.out.println("Answer (CS163): " + rec.backString("CS163"));
}


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


CS Banner
CS Building