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.
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.
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:
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..
public static int countUp(int n): returns the sum of every positive integer from 1 to n, inclusive. Precondition: n is positive.
public static void printDivisors(int n): prints out all of the divisors of n. Precondition: n is positivepublic static boolean isPrime(int n): returns whether n is a prime number or not. Precondition: n is positive. This method will need a helper method.
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.
public static boolean isPalindromeQueue(String s): returns true if s is a palindrome (the same backwards as forwards), and false otherwise. The empty string ("") is a palindrome.
public static String upperCase(String s, String c): returns a version of s, with every instance of c changed to upper-case. Precondition: c is a String of length 1 containing a lower-case letter. As an example, Lab5.upperCase("apples", "p") would return "aPPles".
public static String removeVowels(String s): returns a version of s, with every vowel letter removed. As an example, Lab5.removeVowels("apples") would return "ppls".
Implement the following methods in class Lab5:.
RESTRICTIONS: you may not use any loops (for, while) in your solutions..
public static int sum(int[] a): returns the sum of every element in a. This method will need a helper method.
public static boolean isPalindrome(int[] a): returns whether a is a palindrome. This method will need a helper method.
public static countNegatives(int[] a): returns the number of strictly negative values in a. This method will need a helper method.