Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code In Java Here is the NonRecursiveStringHelper: public class NonRecursiveStringHelper { public static String valueOf(int num) { return +num; } public static String toUpperCase(String str)

Code In Java

Here is the NonRecursiveStringHelper:

public class NonRecursiveStringHelper {

public static String valueOf(int num) {

return ""+num;

}

public static String toUpperCase(String str) {

return str.toUpperCase();

}

public static boolean startsWith(String str, String prefix) {

return str.startsWith(prefix);

}

public static String replace(String str, char oldChar, char newChar) {

return str.replace(oldChar,newChar);

}

public static boolean equals(String str1, String str2) {

return str1.equals(str2);

}

public static int compareTo(String str1, String str2) {

return str1.compareTo(str2);

}

public static int countChar(String str, char ch) {

int count = 0;

for (int i=0; i

if (str.charAt(i) == ch) {

count++;

}

}

return count;

}

}

Recursion

In this assignment, you will write seven static methods in a single class named StringHelper as described below, following the Recursion Rules specified. You will write a test program called StingHelperTester that will test each method in StringHelper.

Attached is sample code (NonRecursiveStringHelper.java) to provide you with a class to write tests against for this assignment. Note that the implementations in the sample do not use recursion and use String class methods your solutions are not permitted to use (such as toString), and may not be correct around edge cases (such as null inputs). I recommend comparing your recursive results to these and the specifications below when debugging your code.

Specification

Define a class named StringHelper that contains the following seven static

methods. You are to use a public/private method pattern.

String valueOf(int num)

Write a recursive method called valueOf that takes an integer num, and returns the String that is the String representation of that number. Each recursive call will add at most one character to the resulting string.

For example, valueOf(1234) returns the String "1234", and valueOf(-123)

returns the String "-123".

Hint: the modulo and division operators will help you break up the integer value into its

individual digits.

Your solution must follow the rules below. Each recursive call will add at most one digit to the

string.

String toUpperCase(String str)

Write a recursive method named toUpperCase that takes a String and returns the uppercase form of the input string. That is, any instance of the characters 'a'-'z' is replaced by the corresponding character 'A'-'Z', and all other characters are unchanged. Your implementation must follow the Recursion Rules specified below. Each recursive call will add

at most one character to the resulting string. Pre: Throw an IllegalArgumentException if passed a null value.

boolean startsWith(String str, String prefix)

Write a recursive method named startsWith that takes two strings, a string and a prefix, and returns true if the string starts with the prefix, and false otherwise. indexOf may not be used. Each recursive call will check at most one character in the string.

Pre/Post: A null or empty prefix always results in a true result; a null or empty string with a non-empty prefix will always result in a false result.

String replace(String str, char oldChar, char newChar)

Write a recursive method named replace that takes a string and two characters, and returns a new string that is the same as the input string except that all instances of oldChar are replaced by

newChar.

indexOf cannot be used on the input string.

A null or empty input string, or one that does not contain oldChar, is returned unchanged.

boolean equals(String str1, String str2)

Write a recursive method named equals that takes two strings and returns true if the two strings hold the same values, and false otherwise. indexOf may not be used. Each recursive call will check at most one character in the string.

Pre: If both are null, this returns true; otherwise if only one is null, false would be returned.

int compareTo(String str1, String str2)

Write a recursive method named compareTo that takes two strings and returns 0 if the two strings hold the same values, a negative value if str1's value is less than str2's in dictionary ordering, and a positive value if str1's value is greater than str2's in dictionary ordering. indexOf may not be used. Each recursive call will check at most one character in the string.

Challenge: return the same values that String's compareTo returns (see the official String.compareTo javadoc)

Pre: If either is null, a IllegalArgumentException is returned.

int countChar(String str, char ch)

Write a recursive method named countChar that takes a string and a character and returns the count of the number of occurrences of the character in the string. indexOf may not be used. Each recursive call will check at most one character in the string.

Pre: If str is null, a 0 is returned.

Recursion Rules

The methods must not use any loops at all, and must use recursion. You cannot call the existing String methods by the same name as the ones here, and you cannot call indexOf, toString, or

toCharArray. You must not use StringBuilder or StringBuffer in your code. (Hint: charAt is the most useful call for solving this, and substring for the recursion.) Your recursive method will generally take the first character of an input string and then recurse on the remaining portion of the string. Your class must not have any instance or static fields, but may define constants to satisfy the code conventions. You may write private helper methods if additional parameters are useful or to limit checks that only need to be made once.

Challenge: Consider performance, do the recursion without using substring.

Tests

Write tests for your seven methods. Name this class StringHelperTest.java. Your tests need to validate these requirements. That means they need to ensure the methods work correctly, and that

they do not work incorrectly. Note that the sample, non-recursive StringHelper provided would actually fail some of the tests as it is missing some of the finer details specified here. You may

write more than one test for each method, in fact it would be unusual if you did not.

For example, I wrote 4 tests for the countChar method:

Test #1: Does countChar produce correct out put for a character known to be in the input string.

Test #2: Does countChar work correctly if the input string is null.

Test #3: Does countChar work correctly if the test character is not in input string.

Test #4: Does countChar work correctly if the input string is zero-length.

My tests look like this:

// Tests for countChar() String testStr = "foobar";

// expected results:

int count = NonRecursiveStringHelper.countChar(testStr, 'o');

// Test #1: Char is in the input String

int countr = StringHelper.countChar(testStr, 'o');

if (count == countr) {

System.out .println("Test #1 Succeeded, Got Expected = " + count);

}

else {

}

System.out .println("Test#2 Failed, Got = " + countr);

}

// Test #2 precondition str == null

// this is a case where the non-recursive method fails

// whenever testing for a null input, use try/catch so your code doesn't crash

try {

count = NonRecursiveStringHelper.countChar(null, 'o');

}

catch (Exception ex) {

ex.getMessage();

}

try {

count = StringHelper.countChar(null, 'o'); if (count == 0) {

System.out .println("Test#2 Succeeded, Got Expected = " + count);

}

else {

}

}

System.out .println("Test #2 Failed, Got = " + countr);

catch (Exception ex) {

System.out .println("Test #2 Failed, Caught Exception "); ex.getMessage()

}

// Test #3 Character is not found in String

count = NonRecursiveStringHelper.countChar(testStr, 'x');

countr = StringHelper.countChar(testStr, 'x'); if (count == countr) {

System.out .println("Test #3 Succeeded, Got Expected = " + count);

}

else {

}

System.out .println("Test #3 Failed, Got = " + countr);

//Test #4: What happens when you pass a zero-length string count =

NonRecursiveStringHelper.countChar("", 'o');

countr = StringHelper.countChar("", 'o'); if (count == countr) {

System.out .println("Test #4 Succeeded, Got Expected = " + count);

}

else {

}

System.out .println("Test #4 Failed, Got = " + countr);

The output for these tests is:

Test #1 Succeeded, Got Expected = 2

Test #2 Succeeded, Got Expected = 0

Test #3 Succeeded, Got Expected = 0

Test #4 Succeeded, Got Expected = 0

Needless to say, you want your tests to succeed so that you know your code is working.

Public Method/Private Helper Method Pattern

The above methods are public methods in the StringHelper class, the actual recursion will be done

in a private method called from the public method. The public method will be where you test your

pre conditions. Only if the inputs meet your preconditions will you call the private helper method.

For example, consider the countChar method. Its public/private pattern would looks like:

public static int countChar(String str, char ch) {

// Precondition str must be non null if (str == null ) {

return 0;

}

return countCharHelper(str, ch);

}

private static int countCharHelper(String str, char ch) { int count = 0;

// your recursive code goes here

}

Additional Requirements

Your code needs to satisfy our class coding conventions for variable, method, and Class names. I will deduct points for code not properly formatted. Include pre and post conditions documentation for all methods.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 2 Lnai 6322

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215882X, 978-3642158827

More Books

Students also viewed these Databases questions

Question

What are two protocols used by the transport layer?

Answered: 1 week ago

Question

The company has fair promotion/advancement policies.

Answered: 1 week ago