CS1027b Computer Science Fundamentals II

Lab 7

General lab instructions to help labs run smoothly

Upon completion of this lab, you should be able to understand recursive thinking in the solution of a simple problem.

Overview and preparation

Upon completion of this lab, you should be able to understand the linked list and circular array implementations of the Queue ADT. For prepation, review the lecture notes on queues (make sure you understand what a queue looks like when implemented as a linked list, as an array, and as a circular array).

Exercise 0

You should be able to answer the following questions correctly in order to be successful in Exercises 1 and 2.

Consider the string defined by String sample = "CS 1027b"; What would be returned by each of the following method calls?

Exercise 1

Download the file RecursionLab.java. Your task is to fill in the code for a recursive method that prints a string backwards. For example, for the parameter string "Java", your method would print "avaJ". The header of this method is public static void reversePrint(String inString) Note that the method is static, i.e. it is a class method, and will not be invoked on an object. (See the main method to see how it is invoked.)


Exercise 2

Now you will write a recursive method that returns the reverse of a string. For example, for the parameter string "Java"”, your method would return the string "avaJ". The header of your method should be public static String reverseString(String inString) Add this method to the file RecursiveLab.java


Add the following code to the main method in RecursionLab.java to test your reverseString method:

// test reverseString
String revString = reverseString(inString);

Optional exercises (will not be marked)

Write a program that asks the user to input a string and then uses method reverseString to determine whether or not the string is a palindrome. A palindrome is a sequence of characters that reads the same both forwards and backwards, for example mom, radar, racecar.

Finally, write a recursive method with the header public static boolean isPalindrome(String s) that returns true if the parameter string is a palindrome and false if it is not.