Palindrome checker in java. I tried my best to put it in one image but had to br
ID: 3842522 • 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 because I'm fairly new to coding.
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 the java code for the above question,
import java.util.Scanner;
public class assign1
{
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 prgram");
}
public static String getInputLine () //Ask user for input and then returns the line
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter a line of input: ");
String getInputLine = scan.nextLine();
return getInputLine;
}
public static boolean isEmptyLine (String str) // Return TRUE if the paramater is empty or False otherwise
{
if (line != null)
{
isEmptyLine = true;
}
else
{
isEmptyLine = false;
}
}
public static boolean isPalindrome (String str)// Return TRUE if the str is a palindrome or FALSE otherwise
{
int left = 0;
int right = line.length() - 1;
boolean okay = true;
while (okay && left < right){
char ch1 = line.charAt(left);
if (!(Character.isDigit(ch1) || Character.isLetter(ch1)))
{
left ++;
}
else{
ch2 = line.charAt(right);
}
if (!(Character.isDigit(ch2) || Character.isLetter(ch2)))
{
right --;
}
else{
ch1 = Character.toUpperCase(ch1);
ch2 = Character.toUpperCase(ch2);
}
if (ch1 == ch2){
left ++;
right --;
}
else{
okay = false;
}
}
return okay;
}
public static String getPalType (String str)//Determine the type of the palindrome and return either word/phrase/number.
{
int num = 0;
int let = 0;
int length = line.length();
for (int i = 0; i <= length; i++)
{
if (Character.isDigit(line.charAt(i)))
num++;
else if (Character.isLetter(line.charAt(i)))
let++;
}
if (num > 0 && let == 0)
{
getPalType = number;
return getPalType;
}
else if (let > 0 && num == 0)
{
getPalType = word;
return getPalType;
}
else
{
getPalType = phrase;
return getPalType;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.