Part I - Reading input in a loop
Before you start doing any coding, brainstorm between the two
of you on how to go about starting this program. You need to get
your code to repeatedly prompt for a new String until the user
enters an empty line, and then end with a goodbye message. What is
the algorithm that you need to use? Write your algorithm into
comments in the main method of the Palindrome class (as shown in
the class notes). The code you write needs to reproduce the
following transcript (user entries are in BOLD):
Enter a string: 1331
Enter a string: racecar
Enter a string: blue
Enter a string:
Empty line read - Goodbye!
Part II - Checking for a palindrome
Now brainstorm with your partner again - once the user has
entered a String you need to think about how to tell if it is a
palindrome. One way to do this is to compare the characters that
are supposed to match - if all of these characters match, then it
is a palindrome. If any pair does not match, then it is not a
palindrome. Think about the algorithm that does this kind of
"pairwise character" comparison - how would you implement that with
a loop? Work out the algorithm with your partner and put it into
your code, then fill in the code around your algorithm to complete
the task. A sample transcript of what your code should do once both
parts are completed is below. User entries are shown in BOLD.
Enter a string: 1331
1331 is a palindrome.
Enter a string: racecar
racecar is a palindrome.
Enter a string: blue
blue is NOT a palindrome.
Enter a string:
Empty line read - Goodbye!
Do not worry about differences in case, punctuation, spacing
or other issues. Your code should only check whether or not the
string of characters is exactly the same forwards as it is
backwards.
Enter a string: Racecar
Racecar is NOT a palindrome
Enter a string: RACECAR
RACECAR is a palindrome.
Enter a string: A man, a plan, a canal, Panama.
A man, a plan, a canal, Panama. is NOT a palindrome.
Enter a string: y123321y
y123321y is a palindrome.
Enter a string:
Empty line read - Goodbye!
Use the following template for your code:
public class Palindrome {
public static void main(String[] args) {
// TODO - fill in with your code
}
}
NOTE: You MUST use a nested loop for this assignment. There
are other ways to solve this problem, but for this assignment you
must have a loop nested inside another loop. Think about using the
outer loop to read the String and the inner loop to test for the
palindrome.
NOTE 2: For this assignment you need to test whether or not a
String is empty (a blank line). Do NOT compare the String with an
empty String. Instead, test the length of the String - if it's
empty, the length will be 0. You can also use the isEmpty instance
method of String if you know how to use it or would like to look it
up. Comparing the String with an empty String is less efficient
than just checking the length. (Note that to read an empty line on
the console, you must use the Scanner's nextLine() method - the
next() method skips blank space and so will not read an empty
line).