In: Computer Science
Write a recursive method to determine if a String is a palindrome.
The program should contain a String array that you can load several test cases to test your palindrome testing method.
The program should load your String Array with the test data containing the possible palindromes from a text file.
The program should test you application with a text file contained in your project folder
After testing your program with your test cases in the text file, you program should allow the user to test your program independently and interactively using Command Line Interface (CLI).
javadoc Requirement
There is also a javadoc requirement for the Java application projects developed in the course. All Java projects must also contain javadoc documentation with it when submitted. There is an option to create javadocs in Intellij. As long as you have your code commented properly, the javadoc will be automatically generated when you selected the option to generate it in Intellij. I uploaded a pdf document describing how to comment your code to generate your javadoc documentation correctly. There are also two hyperlinks leading to internet tutorials on creating javadoc documentation.
Submit Your Application, your Lab Report, and your text file. Your text file should be in your project folder. You must permit the user to enter their own palindrome after the test in run
Program Code Screenshot :
Sample Output :
Program Code to Copy
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; class Main{ //Method to find whether a palindrome or not static boolean isPalindrome(String s){ if(s.length()<=1){ return true; } if(s.charAt(0)!=s.charAt(s.length()-1)){ return false; } return isPalindrome(s.substring(1,s.length()-1)); } public static void testIsPalindrome(String testFile) throws IOException { //Read the file for test cases BufferedReader br = new BufferedReader(new FileReader(testFile)); String s; boolean pass = true; //Read all lines while((s=br.readLine())!=null){ String data[] = s.split(","); String in = data[0]; boolean result = data[1].equalsIgnoreCase("true"); boolean b =isPalindrome(in); //Check if output matches the expected if(b!=result){ System.out.println(s+" Expected : "+result+", Actual : "+b); pass = false; } } if(pass){ System.out.println("All tests Passed"); } else{ System.out.println("There are failed test cases"); } } public static void main(String[] args) throws IOException { testIsPalindrome("D:/test.txt"); Scanner obj = new Scanner(System.in); System.out.println("Enter the String"); String s = obj.nextLine(); if(isPalindrome(s)) { System.out.println(s+" is palindrome"); } else{ System.out.println(s+" is not a palindrome"); } } }