CSU Banner

CS 163/164, 🀨 Fall 2019 🀨

Lab 18 - Recursion Again

Thursday, Nov. 21st or Friday, Nov. 22nd

Overview

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 a RecursionAgain 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 that 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[]){
	RecursionAgain rec = new RecursionAgain();
	rec.starPattern(5);

	System.out.println();
	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