Palindrome checker in java. I tried my best to put it in one image but had to br
ID: 3841572 • Letter: P
Question
Palindrome checker in java. I tried my best to put it in one image but had to break it in 4. Here is the full assignment: Also, please use the Character class to help determine if you have a digit or alphabetic character. The methods are isDigit and isAlphabetic.Also, please make sure to add lots of comments in it.
In this assignment, you are to determine if an input string is a palindrome and, if it is, what type of palindrome. A palindrome for this assignment is defined as "a number, word or phrase consisting of alphanumeric characters that reads the same frontwards and backwards while ignoring case, punctuation and white space'. For this assignment, create a package named assignl and name your file Assignljava. UML Class Diagram Assign main (String 0): void getInputLine 0: String isPalindrome (String): boolean isEm Line (String): boolean getPal Type (String): String Notes on the UML Class Diagram There is only one class. Assignl Underlining the name of the variable/method in a UML class diagram indicates that the variable or method is static. This means that all methods are static. There are no class level variables. The main is public while the other methods are private. Limitations on Java Classes For this assignment, you are limited to the following Java library classes. 1. Scanner 2. String 3. CharacterExplanation / Answer
Here is code for the question. Please don't forget to answer the question if it helped. Thank you very much.
package assign1;
import java.util.Scanner;
public class Assign1 {
/*main method as given in the question. Gets a line from user and processes till an empty line is entered. If the input is a palindrome, the type of it is determined. */
public static void main(String[] args) {
String line = getInputLine();
while(!isEmptyLine (line)){
if (isPalindrome(line))
System.out.println(""" + line +
"" is a palindrome and a " + getPalType (line));
else
System.out.println(""" + line + "" is not a palindrome");
line = getInputLine();
}
System.out.println("End of program");
}
/*gets a line of text from user */
private static String getInputLine()
{
Scanner scanner = new Scanner(System.in);
System.out.print(" Enter a line: ");
return scanner.nextLine(); //get user input from keyboard and return it
}
/* checks if the given string is a palindrome and returns true if palindrome and false otherwise. Implements the algorithm given in question */
private static boolean isPalindrome(String str)
{
int left = 0, right = str.length()-1;
boolean okay = true;
while(okay && left < right)
{
char ch1 = str.charAt(left);
if(!(Character.isDigit(ch1) || Character.isLetter(ch1))) //if the current character is NOT digit or alphabet, move left index
left++;
else
{
char ch2 = str.charAt(right);
if(!(Character.isDigit(ch2) || Character.isLetter(ch2))) //if current right character is not digit/alphabet move right index
right--;
else
{
//convert boht the characters at left and right index to upper case so it is case- insensitive
ch1 = Character.toUpperCase(ch1);
ch2 = Character.toUpperCase(ch2);
if(ch1 == ch2) //matched, so move to next index, left moves forward, right moves backward
{
left++;
right--;
}
else //didnot match
{
okay = false;
}
}
}
}
return okay;
}
//checks if parameter str is empty line and returns true if its empty and false otherwise
private static boolean isEmptyLine(String str)
{
return (str.isEmpty());
}
//called only when str is a palindrome. It determines the type of the palindrom, whether numeric, or word (having only letters) or phrase (having letters and punctuations)
private static String getPalType(String str)
{
String type;
char ch;
boolean okay = true;
//first check for number type
for(int i=0; okay && i<str.length(); i++)
{
ch = str.charAt(i);
//if every character is either a digit or ., then its numeric type
if(!(Character.isDigit(ch) || Character.isWhitespace(ch) || ch == '.'))
okay = false;
}
if(okay) //it was numeric
type = "number";
else //not a number , should be word or a phrase
{
okay = true;
//check for word type
for(int i=0; okay && i<str.length(); i++)
{
ch = str.charAt(i);
//every character is letter
if(!(Character.isAlphabetic(ch)))
okay = false;
}
if(okay)
type = "word";
else //was not a word, has some punctuations as well
type = "phrase";
}
return type;
}
}
Sample output
Enter a line: This is a test
"This is a test" is not a palindrome
Enter a line: 12345.4321
"12345.4321" is a palindrome and a number
Enter a line: Otto!
"Otto!" is a palindrome and a phrase
Enter a line: Able was I, ere I saw Elba.
"Able was I, ere I saw Elba." is a palindrome and a phrase
Enter a line: Abba
"Abba" is a palindrome and a word
Enter a line:
End of program
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.