Question: 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

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?

sample.length()

sample.charAt(1)

sample.charAt(sample.length()-1)

sample.substring(0, 1)

sample.substring(0,sample.length())

sample.concat(sample)

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.)

Hints

Think about the problem of printing a string in reverse recursively by considering an example string, say "abcd". To print it in reverse:

The first character to be printed would be the character "d" (which is the last character of the string). We are then left with the problem of printing a smaller string, "abc" in reverse. (Note that it's the same problem, only with a smaller string)

Print its last character, "c". We are then left with the problem of printing the smaller string "ab" in reverse.

Print its last character, "b". We are then left with the problem of printing the smaller string "a" in reverse.

Print its last character, "a". We are then left with the problem of printing the empty string in reverse.

Generalize that into an algorithm for the method reversePrint

if the parameter string is empty (the base case) return from the method else print the last character of the parameter string call reversePrint with the substring that does not include the last character as the parameter 

You can simplify the algorithm, by instead using the test if the string is not empty. You should be able to write the algorithm using just the String methods charAt, substring and length.

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

Hints

The algorithm should be

if the parameter string is empty (the base case) return the null string as the result string else result string is the last character of the parameter string, concatenated with the reverse of the substring that does not include the last character return the result string 

You should be able to write the algorithm using just the String methods charAt, substring, concat and length.

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

// test reverseString String revString = reverseString(inString); System.out.println(revString);

RecursionLab.java.

import java.io.*; public class RecursionLab{ public static void reversePrint (String inString){ if (inString.length() > 0) // if string is not empty { // your code goes here } } public static void main(String[] args){ String inString = "abcde"; // test reversePrint reversePrint(inString); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!