In this short assignment, you will read in a string from the console, construct its reversal, and print out the reversal of the string, as well as a message indicating whether the string is a palindrome. You will know everything you need to construct the reversal after the end of Wednesday's lecture. To print the message indicating whether the string is a palindrome, you will need if-statements, which we will cover Thursday. If you want to get a jump on this portion of the short assignment, you can read about if-statements in Section 5.2 of Lewis & Loftus.
Constructing the reversal of a string is pretty easy if you think
about it the right way. You already know how to construct a string
one character at a time; you did that in Short
Assignment 3. To reverse a string, however, in each loop
iteration, you will have to concatenate an appropriate character from
the string you're trying to reverse, rather than the fixed character
'#'. And you'll have to think carefully about how you're
going to concatenate it.
You will almost certainly want to start with the reversal as an empty
string. By an "empty string" we mean a String object
that exists, but the string that it holds has zero characters. The
string literal for an empty string is "", that is, two
consecutive double-quotes with nothing between them (not even a space
character). Note that the following two lines of code are really
different:
reversal refers to no
String object at all. If you tried to call a method
using reversal, say reversal.toUpperCase(),
you would get the dreaded "null pointer exception" and your program
would crash. In the second line, reversal refers to a
String object that has no characters. Here, you
can call a method using reversal. For example,
the result of calling reversal.toUpperCase() would also
be an empty string.
Remember that to determine whether two strings are equal, we use the
equals method. For example, if s1 and
s2 each references a String object, then the
call s1.equals(s2) returns a boolean indicating whether
the strings have exactly same characters in the same order. Since
equality is symmetric, the call s2.equals(s1) would
return exactly the same result. (I remind you about the
equals method in case you have seen Java's
== operator; do not use this operator to
determine whether two String objects have the same
contents. We will see soon enough what the == operator
does when you give it strings.)
A few hints:
nextLine or next method of the
Scanner class.
String
methods length and charAt.
length returns a string length
of, say, n, then the String has
characters at positions 0 through n-1, and
only at these positions. Calling charAt
with a parameter greater than or equal to n or
less than 0 will have dire consequences.
char with a
String in the obvious way.
Here are some sample runs from my version of the program:
Hand in your code, along with at least four runs. One run should be on a string that is a palindrome with at least five characters, one run should be on a string that is not a palindrome, one run should be on a string with exactly one character, and one should be on an empty string. If you write your code simply and elegantly, it will just work correctly on an empty string with no additional effort required on your part.