Java String replace Method Test, Assignments of Computer Science

A java code snippet for testing the replace method of the string class. The method replaces all occurrences of a specified character with another character in a given string. An example of how to write a junit test for the replace method and suggests adding more assertions to test various scenarios.

Typology: Assignments

Pre 2010

Uploaded on 08/31/2009

koofers-user-wya
koofers-user-wya 🇺🇸

8 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 127A 10-Sep Section Leader ___________ Name ________________________
Collaboration Work in teams of 2 or 3 (each person must submit this page for credit)
Problem Given the following documentation for the String replace class, complete the test method so
there are enough assertions to convince you that replace works.
// Returns a new string resulting from replacing
// all occurrences of oldChar in this string with newChar.
public String replace(char oldChar, char newChar)
If the character oldChar does not occur in the character sequence represented by this String object, then a
reference to this String object is returned. Otherwise, a new String object is created that represents a character
sequence identical to the character sequence represented by this String object, except that every occurrence of
oldChar is replaced by an occurrence of newChar. Example:
"mesquite in your cellar".replace('e', 'o') returns "mosquito in your collar"
// A Unit test to demonstrate the replace method of class String
// Programmer C Sc 127A
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TwoMethodsTest {
@Test
public void testStringReplace() {
// 0. Test replace when one character is replaced with a different character
String s1 = "abcde";
assertEquals("abTde", s1.replace('c', 'T'));
// Add at least five more assertions for these circumstances
// 1. When more than one character is replaced with a different character
// 2. When no characters are replaced
// 3. When only the first and last characters are replaced with newChar
// 4. When all characters are replaced with newChar
// 5. Make sure the original string did not change after oldChar was found
}
pf2

Partial preview of the text

Download Java String replace Method Test and more Assignments Computer Science in PDF only on Docsity!

C Sc 127A 10-Sep Section Leader ___________ Name ________________________

Collaboration Work in teams of 2 or 3 (each person must submit this page for credit)

Problem Given the following documentation for the String replace class, complete the test method so

there are enough assertions to convince you that replace works.

// Returns a new string resulting from replacing // all occurrences of oldChar in this string with newChar. public String replace (char oldChar, char newChar)

If the character oldChar does not occur in the character sequence represented by this String object, then a

reference to this String object is returned. Otherwise, a new String object is created that represents a character

sequence identical to the character sequence represented by this String object, except that every occurrence of

oldChar is replaced by an occurrence of newChar. Example:

"mesquite in your cellar".replace('e', 'o') returns "mosquito in your collar"

// A Unit test to demonstrate the replace method of class String // Programmer C Sc 127A import static org.junit.Assert. assertEquals ; import org.junit.Test; public class TwoMethodsTest { @Test public void testStringReplace() { // 0. Test replace when one character is replaced with a different character String s1 = "abcde"; assertEquals ("abTde", s1.replace('c', 'T')); // Add at least five more assertions for these circumstances // 1. When more than one character is replaced with a different character // 2. When no characters are replaced // 3. When only the first and last characters are replaced with newChar // 4. When all characters are replaced with newChar // 5. Make sure the original string did not change after oldChar was found }