CSC148H lab -- week 5


Here are the instructions for the week 5 CSC148H lab. To earn your lab mark, you must actively participate in the lab. As always, you will work as a pair, taking turns being the driver and the navigator. The driver types, and the navigator watches for mistakes, thinking ahead. At the end of each method (not each Exercise!), switch roles. At the end of each Exercise, show your TA what you've done.

This week's exercises are on the topic of recursion.

Getting started

When it's your first turn as driver, begin by making a new folder "lab5". (It's OK to use just one partner's account, to save retyping completed work.) In lab5, create a new class called Lab5 and save it as Lab5.java. All of the methods you'll be writing for this lab will be added to this class.

Recursion review/advice

When approaching a problem recursively, remember that a recursive solution is programming language-independent. For these exercises, discuss with your partner how to approach the problem. Write out the recursive definition of your solution in the form of comments within your method. Then, when you commence coding the solution, all you have to do is translate your recursive solution into Java. Programming recursively from scratch is very difficult!

When developing a recursive solution, always start with the base cases. These can usually be defined without knowing the full definition of your recursive solution - for example, in Exercise 2 it's easy to determine if a String of length 0 or 1 is a palindrome. This is regardless of how to decide on a String of length 2 or greater!

When designing the recursive case(s) of your solution (i.e. the part which recurses), remember that your goal is to reduce the problem to a base case. Otherwise, you would recurse infinitely (or at least until the run-time stack fills up)! In some cases, especially "decision" problems, you can sometimes stop early - for example, in Binary Search we sometimes find what we're looking for before we've reduced the problem to a range of 1 or 2 numbers.

In summary, your recusive solution should have two parts:

Exercise 1: Number Recursion

Implement the following methods in class Lab5. Don't forget to develop a recursive solution first, and then test each one when you're finished! Remember, because they're static methods, you can test them easily in the DrJave Interactions Pane, for example: Lab5.isPrime(17).

RESTRICTIONS: you may not use any loops (for, while) in your solutions..

Exercise 2: String Recursion

Implement the following methods in class Lab5. Don't forget to develop a recursive solution first, and then test each one when you're finished! Remember, because they're static methods, you can test them easily in the DrJave Interactions Pane, for example: Lab5.isPalindrome("abba").

RESTRICTIONS: you may not use any loops (for, while) in your solutions. You may only use the following methods from the String class: charAt, substring, concat, length, equals, toUpperCase, toLowerCase, indexOf. You may also use the + operator as a shortform for concat.

Exercise 3: Array Recursion

Implement the following methods in class Lab5:.

RESTRICTIONS: you may not use any loops (for, while) in your solutions..