In: Computer Science
java from control structures through objects 6th edition, programming challenge 5 on chapter 16
Write a boolean method that uses recursion to determine whether a string argument is a palindrome. the method should return true if the argument reads the same forward amd backword. Demonstrate the method in a program, use comments in the code for better understanding. there must be a demo class and method class
also ..
generate javadocs through eclipse compiler. And
make a UML diagram with 3 rows and 1 column
like this bellow.... use (-) for private and (+) for public in the diagram
class name |
attributes |
operations |
here is another example of how the UML diagram should look
https://d2slcw3kip6qmk.cloudfront.net/marketing/pages/chart/uml/class-diagram/class-diagram-object-334x224.PNG
Palindrome class UML diagram :
Java Code:
/**
*
* This class contain a boolean method that uses recursion to
determine whether
* a string argument is a palindrome. the method should return true
if the
* argument reads the same forward amd backword
*
*/
public class Palindrome {
private String inputString;
/**
* Field constructor
* @param inputString
*/
public Palindrome(String inputString) {
this.inputString =
inputString;
}
/**
* @return the inputString
*/
public String getInputString() {
return inputString;
}
/**
* @param inputString the inputString to set
*/
public void setInputString(String inputString) {
this.inputString =
inputString;
}
/**
* This method is accessible to public determine
whether
* input String is a palindrome
* @return
*/
public boolean isPalindrome(){
return
isPalindromeRecursion(this.inputString);
}
/**
* This method determine whether input String is a
palindrome using
* recursion.
*
* @param inputString
* @return true if inputString is palindrome.
*
*/
private boolean isPalindromeRecursion(String
inputString) {
// Check for empty or single
character string & it must be a palindrome
if (inputString.length() == 0 ||
inputString.length() == 1) {
return
true;
}
// Check for recursive call to
determine whether input String is a
// palindrome
if (inputString.charAt(0) ==
inputString.charAt(inputString.length() - 1)) {
return
isPalindromeRecursion(inputString.substring(1, inputString.length()
- 1));
}
return false;
}
}
/**
* The Palindrome Driver Class
*
*/
public class PalindromeDriver {
/**
* Class driver main method
*
* @param args
*/
public static void main(String[] args) {
Palindrome palindrome = new
Palindrome("ABC");
System.out.println(palindrome.getInputString()+":"+palindrome.isPalindrome());
palindrome.setInputString("ABA");
System.out.println(palindrome.getInputString() + ":" +
palindrome.isPalindrome());
palindrome.setInputString("ABAC");
System.out.println(palindrome.getInputString() + ":" +
palindrome.isPalindrome());
palindrome.setInputString("ABAG");
System.out.println(palindrome.getInputString() + ":" +
palindrome.isPalindrome());
palindrome.setInputString("");
System.out.println(palindrome.getInputString() + ":" +
palindrome.isPalindrome());
palindrome.setInputString("B");
System.out.println(palindrome.getInputString() + ":" +
palindrome.isPalindrome());
}
}
Javadoc: