Question: Use java language to solve the following question. Check if a word is a palindrome The following code gets input from the user, which is
Use java language to solve the following question.
Check if a word is a palindrome
The following code gets input from the user, which is saved as a String. Copy this code into a new Processing script and run it to see what it does.
import javax.swing.JOptionPane;
String fourString;
boolean isPalindrome = false;
boolean not4Letters = false;
void setup() {
size(700,200);
background(255);
fourString = JOptionPane.showInputDialog("Type in a four-letter word");
}
void draw() {
checkPalindrome();
/*
Sub-step 2: display either the word, if is a four-letter anagram,
or an error message. Word in small blue text, error in large red text.
The error message should either be about the length not being 4, or that
the word is not a palindrome.
Display text starting at the center of the screen. You get to choose the
text sizes and exact colours.
*/
}
void checkPalindrome() {
/*
Sub-step 1: complete this function. Set isPalindrome to true if the word has
four letters, and if the first and fourth letters are the same and the
second and third letters are the same. If the user didn't enter a four
letter word set not4Letters to true.
*/
}
You need to check if the word is a palindrome: a word that is the same if you reverse the letters (like anna or pop or ABBA). The user is instructed to only type in a four-letter word, but users dont always do what they are supposed to do. So, you need to check that they did type in a four letter word, by checking the length of the String. If the user typed in a four letter word AND the word is a palindrome, you need to display the word on the canvas, otherwise display an error message, as shown below.
Example 1: User types in anna:
Step 3.a Complete the checkPalindrome() function. Set the boolean isPalindrome appropriately. It should be true if the word has four letters AND is a palindrome.
Step 3.b After checkPalindrome() is called in draw(), display the right thing on the Canvas (either the word in small blue text if its a palindrome, or the error message XXXX is NOT a palindrome in large red text (where XXXX represents the word the user typed in).
Step 3.c Improve the code above by finding out the width of the text you want to print out and using that to center the text on the Canvas. If you type in something that isnt a palindrome, you should then see output like this:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
