Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume a developer writes the following source code to concatenate two strings.

ID: 3736675 • Letter: A

Question

Assume a developer writes the following source code to concatenate two strings.

/* the method is used to concatenate too strings into one. It returns a string that represents the concatenation of this object's characters followed by the string argument's characters.*/

public String concat(String str) {

int otherLen = str.length();

int len = value.length;

char buf[ ] = Arrays.copyOf(value, len + otherLen);

str.getChars(buf, len);

return new String(buf, true);

}

The expected behavior of the method is to concatenate two strings into one. For example, assume we have two strings s1 and s2. s1= “John” and s2= “Smith”.

The concat( ) method can be used to concatenate s1 and s1into s3---the full name of the person (John Smith) by the following statement:

          String s3= s1.concat (s2);

Write a unit test to test the method’s behavior (do not focus on the source code itself, instead, your unit test will test the behavior/outcome of the method). Write the unit test using

1) any programming languages, or

2) pseudo code, or

3) English statements, or

4) combination of the above.

The unit test needs to include the steps/statements of how it tests the behavior of the concat() method.

Explanation / Answer

The unit test for the concat function will be as follows:

s1.concat(str2)

1.str2 = NULL . The return value will be s1 itself

2. str2 = "". The return value will be s1 itself

3. str2 = "somestr2". The return value will be (if say s1 = "somestr1"),
           "somestr1 somestr2"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote