Question
Write Java code that prompts the user to enter one or more words and prints whether the entered String is a palindrome (i.e., reads the
Write Java code that prompts the user to enter one or more words and prints whether the entered String is a palindrome (i.e., reads the same forwards as it does backwards, for example, "abba" or "racecar").
Make the code case-insensitive, so that words like "Abba" and "Madam" will be considered palindromes. Consider that "abca" should not be a palindrome.
Complete the provided class Palindrome.java.
import java.util.*;
/** * Determines if a user entered String is a palindrome, which means the String * is the same forward and reverse. The determination is case-insensitive. * Spaces and punctuation must match for palindromes. * * */ public class Palindrome {
/** * Starts program * * @param args array of command line arguments */ public static void main(String[] args) { printPalindrome("abba");// palindrome printPalindrome("racecar");// palindrome printPalindrome("Madam");// palindrome printPalindrome("!!!Hannah!!!");// palindrome printPalindrome("! ! ! Hannah ! ! !");// palindrome printPalindrome("Dot saw I was tod");// palindrome printPalindrome("Step on no pets");// palindrome printPalindrome("Step on no pets!"); // NOT printPalindrome("dog"); // NOT printPalindrome("Java"); // NOT printPalindrome("abca"); // NOT System.out.println(" *****Read from User*****"); readConsole(); }
/** * Reads user's console input and prints the results */ public static void readConsole() { Scanner in = new Scanner(System.in); System.out.print("Enter a word or phrase: "); String text = in.nextLine(); printPalindrome(text); }
/** * Prints whether text is a palindrome * @param text String to check whether it is a palindrome */ public static void printPalindrome(String text) { System.out.print("isPalindrome: \t"); if (isPalindrome(text)) { System.out.println("\"" + text + "\" is a palindrome"); } else { System.out.println("\"" + text + "\" is NOT a palindrome"); } }
/** * Returns true if the text is a palindrome - the same word forward and * backwards * * @param text String object to test if palindrome * @return whether text is a palindrome */ public static boolean isPalindrome(String text) { // TODO: Complete method return true;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started