Question: I created a recursive and iterative palindrome program that determines whether the string entered is a palindrome or not. a) create a list of test

I created a recursive and iterative palindrome program that determines whether the string entered is a palindrome or not.

a) create a list of test cases to be run against both palindrome checkers.

recursive

import java.io.*; public class RecursivePalindrome { static boolean CheckPalindrome(String s, int leftSide, int rightSide) { if (rightSide <= leftSide) return true; else if (s.charAt(leftSide) != s.charAt(rightSide)) return false;

else return CheckPalindrome(s,leftSide+1,rightSide-1);

} public static void main(String[] args) throws IOException {

String str; int n;

InputStreamReader inStream = new InputStreamReader( System.in ); BufferedReader stdin = new BufferedReader( inStream ); System.out.print("Please enter any string: "); str = stdin.readLine();

int lastPosition = str.length()-1;

boolean result = CheckPalindrome(str , 0, lastPosition); if (result)

System.out.println("The string \""+str+"\" is a palindrome ");

else

System.out.println("The string \""+str+"\" is not a palindrome ");

}

}

iterative

import java.util.Scanner; public class Iterative { public static void main(String args[]) {

Scanner reader=new Scanner(System.in); System.out.println("please enter a string"); String input=reader.nextLine();

System.out.printf("Is %s a palindrome? : %b %n", input, isPalindrome(input));

System.out.println("please enter another string"); input=reader.nextLine();

System.out.printf("Is %s a palindrome? : %b %n", input, isPalindrome(input));

reader.close();

} public static boolean isPalindrome(String input) { if(input==null || input.isEmpty()) { return true; }

char[] array=input.toCharArray(); StringBuilder sb=new StringBuilder(input.length()); for(int i=input.length() -1; i>=0; i--) { sb.append(array[i]);

} String reverseOfString=sb.toString(); return input.equals(reverseOfString);

} }

b) list of test cases for the palindromic date generator. identifies al palindromic dates in a given year. first a user enters a year. then the program reports the palindromic dates. finally, the program asks the user if he or she wishes to try again.

palindromicdate

import java.util.Scanner;

public class palindromicdate {

// method to get String representation of a month number public static String getMonthStr(int m) {

if (m < 10) return "0" + m; else return "" + m; }

// method to get String representation of a day number public static String getDayStr(int day) { if (day < 10) return "0" + day; else return "" + day; }

// method to get number of days in month public static int getNumDays(int m, int year) { switch (m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 2: if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) return 29; else return 28; default: return 30; } }

// function to get month name public static String getMonthName(int month) { String monthStr = ""; switch (month) { case 1: monthStr = "January"; break; case 2: monthStr = "February"; break; case 3: monthStr = "March"; break; case 4: monthStr = "April"; break; case 5: monthStr = "May"; break; case 6: monthStr = "June"; break; case 7: monthStr = "July"; break; case 8: monthStr = "August"; break; case 9: monthStr = "September"; break; case 10: monthStr = "October"; break; case 11: monthStr = "November"; break; case 12: monthStr = "December"; break; default: monthStr = ""; } return monthStr; } // function to check whether a string is palindrome or not public static boolean isPalindrome(String str){ int len = str.length(); for(int i=0,j=len-1; i < j; i++, j--) if(str.charAt(i) != str.charAt(j)) return false; return true; }

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); while (true) {

System.out.print("Enter a year: "); int year = sc.nextInt();

for (int month = 1; month <= 12; month++) {

String monthStr = getMonthStr(month);

for (int day = 1; day <= getNumDays(month, year); day++) { String daeStr = monthStr+getDayStr(day)+year; if(isPalindrome(daeStr)){ System.out.println(" "+getMonthName(month)+" "+day+", "+year); } } } System.out.print("Do you want to test more year(yes/no)? "); String op = sc.next(); if(! "yes".equalsIgnoreCase(op)) break; } } }

c) solution description for palindromicdate

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!