starting foreach
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 madamiamadam
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:
Yes, if the string holds the property, and
As is the case with any non-trivial problem, there are more than ways 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:
reverseString()
that returns a copy of the input string containing its characters in reverse order.theSame()
that compares two input strings (the original and the reversed copy) character-by-character. If all characters match, then it returns true. If not, it returns false.Work the above strategy through a couple of examples and make sure you understand it before continuing.
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.
Run the produced pseudocode by hand on your examples. If you discover errors in the logic, fix them and repeat this step.
Only now you are ready to start writing Java code.
Create a new project and add a class named FunWithStrings
. In there:
Add a main()
method to hold your testing as you go. The main() is the only static method in your program, the rest should not be. This means that you should create and use a dummy object:
FunWithStrings f = new FunWithStrings();
Define a fruitful method reverseString()
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.
Define a predicate method theSame()
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.
Do not use the equals() neither the compareTo()
methods from the String class here.
Write the (top level) predicate method sameBackAndForth()
that takes as input a string and determines whether it reads the same L2R and R2L.
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
.
PseudocodeL2RnR2L
(with the appropriate file extension) containing your pseudocodeFunWithStrings.java
containing your java codeTestingL2RnR2L.txt
containing your test resultsUse the FunWithStringsTest.java code we provide here.