Homework 2, Part A: Fun with Strings

Goals:

  1. to work with Java’s Strings.
  2. to practice method definitions and method invocations
  3. to practice with extensive and appropriate testing
  4. to practice with loops

Task: L2R and R2L

Step 1: Study the problem

As you know, in order to understand a piece of text in a human language you need to read it either left-to-right (L2R), like in Anglophone languages, or right-to-left (R2L), like in Hebrew or Arabic. However, there are pieces of text that read exactly the same R2L and L2R, in the same language! Such texts are dad and madamimadam in English, and νιψονανομηματαμημονανοψιν (“wash your sins, not only your face”) in Greek. Note that we only use lower case letters and no spaces.

In this problem you are asked to write a program to decide whether a given String holds the property of reading the same from L2R and R2L, and report the result:

Step 2: The plan

As is the case with any non-trivial problem, there are more than one way to solve it. We encourage you to try to think of one on your own.

However, for this task, you need to implement the plan described here:

We recommend working through the above strategy a couple of examples and make sure you understand it before continuing.

Step 3: Write pseudocode

At this point you are ready to express the strategy of solving this problem in pseudocode. Write it in your notebook, and take a picture of it. You will submit it at the end, together with the other files. Or, if you are using an electronic device to write, save your work in a file to submit at the end.

Step 4: Hand-execute the pseudocode

Run the produced pseudocode by hand on your examples. If you discover errors in the logic, fix them and repeat this step.

Step 5: Time to code!

Only now you are ready to start writing Java code.

Create a new project and add a class named FunWithStrings. In there:

  1. Add a main() method to hold your testing as you go.

  2. Add method public static String reverseString(String s) that takes as input a string and returns another string with its characters in reverse order. Test this method to make sure it works as intended before moving to the next one.

  3. Add a predicate method public static boolean theSame(String s1, String s2) that takes two String inputs and checks whether they contain identical characters, one by one. If and only if the two strings are the same, the method should return true. For the purposes of the exercise, please do not use the equals() or the compareTo() methods from the String class here.

  4. Add a predicate method public static boolean sameBackAndForth(String s) that takes as input a string and determines whether it reads the same L2R and R2L.

  5. Choose the testing cases thoughtfully. Use testing strings of various lengths (like 0, 1, 2, 3 and a longer one). Some of the testing strings should read the same in both directions, others not.

It is expected that your code has careful and meaningful documentation (top of the file and top of methods javadoc, as well as inline documentation). Top of the file javadoc should include values for the @author and @version tags. Top of the method javadoc should include values for the @param and @return tags as needed.

In BlueJ produce the TestingL2RnR2L.txt file to contain testing results from running the top level method (sameBackAndForth()). (Terminal Window –> Options –> Save to file…)

Testing results should include both expected and computed outcome, like: input: dad. Expect: true Computed: true.

Submission:

  1. Submit the following files (only):
    • PseudocodeL2RnR2L.pdf containing your pseudocode
    • FunWithStrings.java containing your java code
    • TestingL2RnR2L.txt containing your test results

Check if your code passes our basic tests (optional)

Use the code below to test your implementation. To do this, create a new class, FunWithStringsTest.java, and replace its content with the code below. After compiling all of your code, write click on the class in the project viewer, and select “Test All.”

FunWithStringsTest.java:

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class FunWithStringsTest {

    @Before
    public void setUp() {
        // N/A
    }

    @Test
    public void testReverseString() {
        assertEquals("edcba", FunWithStrings.reverseString("abcde"));
        assertEquals("gfedcba", FunWithStrings.reverseString("abcdefg"));
        assertEquals("", FunWithStrings.reverseString(""));
        assertEquals("a", FunWithStrings.reverseString("a"));
    }

    @Test
    public void testTheSame() {
        assertTrue(FunWithStrings.theSame("abc", "abc"));
        assertFalse(FunWithStrings.theSame("abc", "abcd"));
        assertFalse(FunWithStrings.theSame("abc", "abC"));
        assertFalse(FunWithStrings.theSame("abc", "ab"));
    }

    @Test
    public void testSameBackAndForth() {
        assertTrue(FunWithStrings.sameBackAndForth("aba"));
        assertTrue(FunWithStrings.sameBackAndForth("abcba"));
        assertFalse(FunWithStrings.sameBackAndForth("abca"));
        assertFalse(FunWithStrings.sameBackAndForth("abcde"));
    }
}


Homework 2, Part B: More String Operations

Goals:

  1. to work with some of Java’s built-in String class methods
  2. to refresh your memory on recursion

Task:

Create a new class named StringOps and include definitions for the following (static) methods:

Finally, save the results of your program into a textfile named StringOpsTesting.txt. You will need to submit this text file along with the source code.

Note:

Submitting

When done, submit your StringOps.java along with the StringOpsTesting.txt that shows the results of your comprehensive testing. Please DO NOT submit any other file but these two.


Submission Checklist