CSU Banner

CS 163/164, Fall 2017

Lab 18 - Recursion

(Thursday, Nov. 9th || Friday, Nov. 10th)


This lab will explore and get good practice with recursion! We will be doing a few tasks, all using recursion - NO LOOPS ALLOWED! For this assignments you will create an R18 project and class. The class will implement the following interface: IR18.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.

First, we will be completing the code for creating a star pattern. There are two methods tha work together here. The
starString(int x)
method is a recursive method that returns a string with the appropriate number of stars to
starPattern(int x)
to be printed. EXAMPLE OUTPUT IF X == 5:

Next, we will implement the method
palindrome(String test)
that accepts a string and returns true or false depending on whether it is a palindrome or not. You can use any method of the String class that you think will be useful.

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

Below is the test code for the TA to verify.

public static void main(String args[]){
	R18 rec = new R18();

	System.out.println ("\'x\' is a palindrome?: " + rec.palindrome("x"));
	System.out.println("\'car\' is a palindrome?: " + rec.palindrome("car"));
	System.out.println("\'racecar\' is a palindrome?: " + rec.palindrome("racecar"));
	System.out.println("\'hannah\' is a palindrome?: " + rec.palindrome("hannah"));
	System.out.println("\'banana\' is a palindrome?: " + rec.palindrome("banana") + "\n");

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

CS Banner
CS Building