Program #3

Due Friday, Feb.16, 2:00 pm


Purpose

To learn about Strings and StringBuffers.
To learn to get input from user using line-oriented (non-GUI) I/O.
To gain practice writing an Application (note -- this is not an Applet).

Assignment

You will write an Application which accepts two strings the user types in (give them a nice prompt). The results of the tests will be displayed to the screen. Once the user has input two strings your program will perform the following tests on the two strings:

Is the second string a substring of the first string (capitalization is not important)?
Is the first string a palindrome? Is the second string a palindrome? (A palindrome is spelled the same way backwards as forwards, ignoring case and spacing.)
Is the second string an anagram of the first string (the same letters of the first in a different order)?

Details

Your Application should perform each of the three checks in the order above.   These "check" methods should not actually print anything on the screen; instead, they should simply return a boolean, and the main method should use that information to produce the output.

If your program identifies that the second is a sub-string of the first, it should produce the message stating that. For instance, if the first string is "The woods were dark and foreboding" and the second was "Dark andfor", your program should produce a message that says that the second is a substring of the first. If the second string is not a substring, then produce a message which states that fact.  Ignore spacing and capitalization.

Your program should be able to recognize palindromes. For example, "Madam Im Adam" is a palindrome (same backwards as forwards, ignoring spacing and capitalization).

Finally, if your program identifies that the second string is an anagram of the first string, that message should be displayed.  If it is not, then it will display an appropriate message. For example, it should identify that "a soft felt cat" is an anagram of "as oft left act", but that "left act" is not an anagram of "a soft felt cat." Ignore spacing and capitalization.
 

Example

Here is what your screen should look like if the sample inputs below are entered:
   Please enter string 1:
   mad ma
   Please enter string 2:
   madam

   Results of tests:
   madam is not a substring of mad ma
   mad ma is not a palindrome
   madam is a palindrome
   madam is an anagram of mad ma

Hints

You might consider the following step-wise development: Think about the methods for checkSubstring, checkAnagram, and Palindrome. Consider what the arguments, returns, and actions should be for these. Finally, implement each of the three check methods one at a time.

Your class should have instance variables for the two strings.

You may find it helpful to convert each string to an array of characters for some of the required processing.

Chapter 2's "Java Language Summary" section has information on reading input from the keyboard, including a thoroughly explained example. Pay particular attention to what it suggests regarding exception handling. (We'll be talking in more detail about exception handling later, but the simple solution they suggest is sufficient for now.)

You will probably find the Sun website very helpful for finding useful String methods.

Turn In

Turn in electronically by 2:00pm 2/16/2001: