PreAP Computer Science The "Palindrome" Program Text Lab 07 Practice/Perform Major Assignment 80, 90, 100 & 110 Point Versions Assignment Purpose: This program requires knowledge and manipulation of Java String objects. Write a program that determines if an entered string is a Palindrome. True palindromes are strings of characters that read the same backward as forward. This does mean all characters, including spaces and punctuations. Examples of palindromes are: MADAM, RACECAR, BOB, HANNAH, CIVIC, KAYAK, LEVEL, REVIVER Text Lab 07 Student Version Do not copy this file, which is provided. // TextLab07st.java // The "Palindrome" Program // This is the student, starting version of Text Lab 07. public class TextLab07st { public static void main (String args[]) { System.out.println("\nTextLab07\n"); boolean finished = false; do { System.out.print("Enter a string ===>> "); String str = Expo.enterString(); System.out.println(); System.out.println("Entered String: " + str); System.out.println("Palindrome: " + Palindrome.isPal(str)); System.out.println("Almost Palindrome: " + Palindrome.almostPal(str)); // used only for the 100 and 110 point versions System.out.println("Least Palindrome: " + Palindrome.leastPal(str)); // used only for the 110 point versions System.out.println(); System.out.print("Do you wish to repeat this program [Y/N]? char repeat = Expo.enterChar(); finished = (repeat != 'Y' && repeat != 'y'); System.out.println(); } while (!finished); ===>> } } Exposure Java 2012, PreAPCS Edition Text Lab 07 Page 1 04-07-12 "); class Palindrome { public static boolean isPal(String s) /* * Precondition: s is an arbitrary String. * Postcondition: The value of true is returned if s is a Palindrome, * false otherwise. */ { return false; } public static String purge(String s) /* * Precondition: s is an arbitrary String. * Postcondition: All non-letter characters are removed from s, * and this new "purged" String is returned. */ { return s; } public static boolean almostPal(String s) /* * Precondition: s is an arbitrary String. * Postcondition: After purging all non-letter characters from s, * the value of true is returned if the resulting * String is a Palindrome, false otherwise. */ { return isPal(purge(s)); // Do not alter this method! } public static String leastPal(String s) /* * Precondition: s is an arbitrary String. * Postcondition: A new String is returned which is created by adding the minimum * number of characters necessary to S to make it a Palindrome. */ { return ""; } } Exposure Java 2012, PreAPCS Edition Text Lab 07 Page 2 04-07-12 80 Point Version Specifics The main thing this program needs to do is determine if an entered string is a Palindrome. To do this, you must complete the isPal method. For this version, the isPal method is case-sensitive meaning while madam and MADAM are palindromes, Madam is not. You also are not concerned checking if the String is an Almost Palindrome and computing the Least Palindrome. The program will generate some output for these, but it can be ignored. 80 Point Version Output For the 80 and 90 Point Versions, Almost Palindrome will always have the same result as Palindrome and Least Palindrome will always be blank. Exposure Java 2012, PreAPCS Edition Text Lab 07 Page 3 04-07-12 90 Point Version Specifics The 90 point version is very similar to the 80 point version except now the isPal method is no longer case sensitive. So madam and MADAM are still palindromes, but now Madam, mADAM, and mADam are palindromes as well. You also are not concerned with checking if the String is an Almost Palindrome or computing the Least Palindrome. The program will generate some output for these, but it can be ignored. 90 Point Version Output For the 80 and 90 Point Versions, Almost Palindrome will always have the same result as Palindrome and Least Palindrome will always be blank. Exposure Java 2012, PreAPCS Edition Text Lab 07 Page 4 04-07-12 100 Point Version Specifics The 100 point version requires everything from the 90 Point version and adds the ability to detect Almost Palindromes. An Almost Palindrome is a sentence or phrase that becomes a Palindrome when you remove any characters that are not letters. A few examples are below: Madam I’m Adam. A man, a plan, a canal, Panama Not A Banana Baton! You may have noticed that the almostPal method is already written for you. Before you get excited about an easy 100, you need to understand how it is written. See the code below: public static boolean almostPal(String s) /* * Precondition: s is an arbitrary String. * Postcondition: After purging all non-letter characters from s, * the value of true is returned if the resulting String is a Palindrome, * false otherwise. */ { return isPal(purge(s)); // Do not alter this method! } This is a very short method. If we look at the one statement in the method body and work our way from the outside in, we see that first we send the value of s to the purge method, which will remove all non-letter characters and then return the “purged” tring, which is then, in turn, sent to the isPal method, which will determine if this is a Palindrome. If the purged string is a Palindrome, then the original string is an Almost Palindrome. If you look at the RaceCar example you be aware of 2 things. First, just like the 90 point version, this version is not case sensitive. Second, for the purpose of this program a word or phrase that already is a Palindrome, like RaceCar, will also be an Almost Palindrome. You are not concerned with computing the Least Palindrome. It will simply be blank. Exposure Java 2012, PreAPCS Edition Text Lab 07 Page 5 04-07-12 100 Point Version Output Since this 4th string has no letters whatsoever, it is guaranteed to be an Almost Palindrome. When all non-letter characters are removed, there is nothing left, and an empty string is a Palindrome. Exposure Java 2012, PreAPCS Edition Text Lab 07 Page 6 04-07-12 110 Point Version Specifics The 110 point version requires everything from the 100 point version and adds the ability to create Least Palindromes. Consider the word Panama. It is not a Palindrome. It is also not an Almost Palindrome. However, any string can be made into a Palindrome if you reverse the string and then concatenate it to the end of the string. So in the case of Panama, the reverse is amanaP. If we concatenate these we get PanamaamanaP which is a Palindrome, but it is not the Least Palindrome. The reason it is not the Least Palindrome is we concatenated more characters than were necessary. Simply by concatenating naP we get PanamanaP which not only is a Palindrome, it is the Least Palindrome. By definition, this means if the entered string was already a Palindrome, like MADAM, then is already is a Least Palindrome. In other words, the Least Palindrome of MADAM is MADAM. 110 Point Version Output Since the first 2 strings MADAM and RaceCar are Palindromes, their Least Palindromes will be exactly the same. Exposure Java 2012, PreAPCS Edition Text Lab 07 Page 7 04-07-12
© Copyright 2025 Paperzz