Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is based on the material on pages 151-154, particularly the Palindrome Finder application described on pages 152-153. Create an application that accepts a phrase

image text in transcribedimage text in transcribedimage text in transcribed

This is based on the material on pages 151-154, particularly the Palindrome Finder application described on pages 152-153. Create an application that accepts a phrase from the user via keyboard and prints out all words from the phrase that are palindromes. TABLE 4.2 Class PalindromeFinder Methods private static Deque fillStack (String inputString) private String buildReverse(String inputString) Behavior Returns a stack that is filled with the characters in inputString Calls fillStack to fill the stack based on inputString and returns a new string formed by popping each character from this stack and joining the characters. Empties the stack Returns true if inputString and the string built by buildReverse have the same contents, except for case. Otherwise, retums false public boolean ispalindrome (String inputString) Design We can define a class called PalindromeFinder (Table 4.2) with three static methods: fillStack pushes all characters from the input string onto a stack, buildReverse builds a new string by popping the characters off the stack and joining them, and isPalindrome compares the input string and new string to see whether they are palindromes. Method i spalindrome is the only public method and is called with the string to be tested as its argument. Implementation Listing 4.2 shows the class. Method ispalindrome calls buildReverse, which calls fillStack to build the stack (an ArrayDeque). In fillStack, the statement charStack.push(inputString.charAt(i)); autoboxes a character and pushes it onto the stack. In method buildReverse, the loop while (!charStack.isEmpty) { // Remove top item from stack and append it to result. result.append(charStack.pop(); } pops each object off the stack and appends it to the result string. Method ispalindrome uses the String method equals Ignorecase to compare the original string with its reverse. return inputString.equalsIgnoreCase(buildReverse(inputString)); LISTING 4.2 PalindromeFinder.java import java.util.*; /** Class with methods to check whether a string is a palindrome. */ public class PalindromeFinder { *** Fills a stack of characters from an input string. @param inputString the string to be checked Greturn a stack of characters in inputString private static Deque fillStack(String inputString) { Deque charStack = new ArrayDeque >0; 4.2 Stack Applications 153 for (int i = 0; i charStack = fillStack(inputString); StringBuilder result = new StringBuilderO; while (!charStack.isEmptyO) { // Remove top item from stack and append it to result. result.append(charStack.pop(); } return result.toStringO; } ** Calls buildReverse and compares its result to inputString * @param inputString the string to be checked * Breturn true if inputString is a palindrome, false if not public static boolean isPalindrome (String inputString) { return inputString.equalsIgnoreCase(buildReverse(inputString)); } } Testing To test this class, you should run it with several different strings, including both palin- dromes and nonpalindromes, as follows: A single character (always a palindrome) Multiple characters in one word Multiple words Different cases Even-length strings Odd-length strings An empty string (considered a palindrome) Listing 4.3 is a JUnit test suite for the PalindromeFinder class. LISTING 4.3 PalindromeFinder Test import org.junit. Test; import static org.junit.Assert.*; Test of the PalindromeFinder * Gauthor Koffman & Wolfgang */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

5. Identify strategies for building coalitions across cultures.

Answered: 1 week ago

Question

What does Processing of an OLAP Cube accomplish?

Answered: 1 week ago