Concepts StringBuilder Var-Args Programming Assignment Palindrome Checker - A palindrome is a string that is the same forwards as backwards. For example, the following strings are palindromes: " ", "a", "aa", "bb", "aba", "bab", "bob" These are not: "ab", "ba", "bba", "abb" Use StringBuilder concept to create a palindrome checker. The method will be called palindromChecker and it will use the var-arg concept to accept 1 to many Strings. The class name for this program is Palindrome.
class Palindrome{
//palindromeChecker method takes zero or more string arguments and prints message accordingly
public static boolean palindromeChecker(String ...str){
for(String s : str){ //takes string one by one
StringBuilder sb = new StringBuilder(s); //creates a stringbuilder for s
sb.reverse(); //reverses the sb
//if any of the string is not palindrome then returns false
if(!s.equals(sb.toString())){
return false;
}
}
return true; //if every string is palindrome then returns true.
}
}
class TestPalindrome{ //driver class named TestPalindrome
//main method takes values passed by command line arguments into args
public static void main(String []args) {
Palindrome palindrome = new Palindrome(); //created an object for Palindrome class
boolean result = palindrome.palindromeChecker("bob", "Sue", "aaa", "Gossai"); //calls palindromeChecker method
System.out.println("All strings entered were palindromes: " + result); //prints the result to user.
}
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.