JAVA Program Modify this program so it passes all the Test cases and it should not use a while loop. Instead try to write a recursive method that doesn't copy the string over and over again. I have provided the failed test cases. import java.util.Scanner; // creating a class class Main { // creating a method that determines whether the given string is palindrome or not public static boolean isPalindrome(String str) { // if length of the given string is 0 or 1 if (str.length() <= 1) { return true; } // otherwise else if (str.charAt(0) == str.charAt(str.length() - 1)) { return isPalindrome(str.substring(1, str.length() - 1)); } else { return false; } } // Driver code public static void main(String[] args) { // scanner object used to take user inputs Scanner sc = new Scanner(System.in); String str; while(true){ System.out.println("Please enter a string to test for palindrome or type QUIT to exit:"); // taking a string and convert it into lower case str = sc.nextLine().toLowerCase(); if (str.equals("quit")) { break; } // otherwise else{ // if method returns true if (isPalindrome(str)) { System.out.println("The input is a palindrome."); } // otherwise else { System.out.println("The input is not a palindrome."); } } } } } Chapter 16. PC #5. Palindrome Detector (page 1073) A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak 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 and backward. Demonstrate the method in a program. The program should ask the user to enter a string, which is checked for palindrome property. The program displays whether the given input is a palindrome or not, then prompts the user to enter another string. If the user enters QUIT (case insensitive, then exit the program). Test Case 1 Please enter a string to test for palindrome or type QUIT to exit:\n Desserts, I stressedENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n KayakENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 2 Please enter a string to test for palindrome or type QUIT to exit:\n dadENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 3 Please enter a string to test for palindrome or type QUIT to exit:\n momENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n QuitENTER Test Case 4 Please enter a string to test for palindrome or type QUIT to exit:\n helloENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quiTENTER Test Case 5 Please enter a string to test for palindrome or type QUIT to exit:\n b,a,dENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 6 Please enter a string to test for palindrome or type QUIT to exit:\n Able was I, ere I saw ElbaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 7 Please enter a string to test for palindrome or type QUIT to exit:\n A man, a plan, a canal, PanamaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 8 Passed! Please enter a string to test for palindrome or type QUIT to exit:\n @abENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n ab@ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n @aaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n aa@ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 9 Passed! Please enter a string to test for palindrome or type QUIT to exit:\n abbaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n abcaENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n aabcaaENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n abaceedabaENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER
JAVA Program Modify this program so it passes all the Test cases and it should not use a while loop. Instead try to write a recursive method that doesn't copy the string over and over again. I have provided the failed test cases. import java.util.Scanner; // creating a class class Main { // creating a method that determines whether the given string is palindrome or not public static boolean isPalindrome(String str) { // if length of the given string is 0 or 1 if (str.length() <= 1) { return true; } // otherwise else if (str.charAt(0) == str.charAt(str.length() - 1)) { return isPalindrome(str.substring(1, str.length() - 1)); } else { return false; } } // Driver code public static void main(String[] args) { // scanner object used to take user inputs Scanner sc = new Scanner(System.in); String str; while(true){ System.out.println("Please enter a string to test for palindrome or type QUIT to exit:"); // taking a string and convert it into lower case str = sc.nextLine().toLowerCase(); if (str.equals("quit")) { break; } // otherwise else{ // if method returns true if (isPalindrome(str)) { System.out.println("The input is a palindrome."); } // otherwise else { System.out.println("The input is not a palindrome."); } } } } } Chapter 16. PC #5. Palindrome Detector (page 1073) A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak 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 and backward. Demonstrate the method in a program. The program should ask the user to enter a string, which is checked for palindrome property. The program displays whether the given input is a palindrome or not, then prompts the user to enter another string. If the user enters QUIT (case insensitive, then exit the program). Test Case 1 Please enter a string to test for palindrome or type QUIT to exit:\n Desserts, I stressedENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n KayakENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 2 Please enter a string to test for palindrome or type QUIT to exit:\n dadENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 3 Please enter a string to test for palindrome or type QUIT to exit:\n momENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n QuitENTER Test Case 4 Please enter a string to test for palindrome or type QUIT to exit:\n helloENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quiTENTER Test Case 5 Please enter a string to test for palindrome or type QUIT to exit:\n b,a,dENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 6 Please enter a string to test for palindrome or type QUIT to exit:\n Able was I, ere I saw ElbaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 7 Please enter a string to test for palindrome or type QUIT to exit:\n A man, a plan, a canal, PanamaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 8 Passed! Please enter a string to test for palindrome or type QUIT to exit:\n @abENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n ab@ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n @aaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n aa@ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER Test Case 9 Passed! Please enter a string to test for palindrome or type QUIT to exit:\n abbaENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n abcaENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n aabcaaENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n abaceedabaENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quitENTER
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
JAVA Program
Modify this program so it passes all the Test cases and it should not use a while loop. Instead try to write a recursive method that doesn't copy the string over and over again. I have provided the failed test cases.
import java.util.Scanner;
// creating a class
class Main {
// creating a method that determines whether the given string is palindrome or not
public static boolean isPalindrome(String str) {
// if length of the given string is 0 or 1
if (str.length() <= 1) {
return true;
}
// otherwise
else if (str.charAt(0) == str.charAt(str.length() - 1)) {
return isPalindrome(str.substring(1, str.length() - 1));
} else {
return false;
}
}
// Driver code
public static void main(String[] args) {
// scanner object used to take user inputs
Scanner sc = new Scanner(System.in);
String str;
while(true){
System.out.println("Please enter a string to test for palindrome or type QUIT to exit:");
// taking a string and convert it into lower case
str = sc.nextLine().toLowerCase();
if (str.equals("quit")) {
break;
}
// otherwise
else{
// if method returns true
if (isPalindrome(str)) {
System.out.println("The input is a palindrome.");
}
// otherwise
else {
System.out.println("The input is not a palindrome.");
}
}
}
}
}
// creating a class
class Main {
// creating a method that determines whether the given string is palindrome or not
public static boolean isPalindrome(String str) {
// if length of the given string is 0 or 1
if (str.length() <= 1) {
return true;
}
// otherwise
else if (str.charAt(0) == str.charAt(str.length() - 1)) {
return isPalindrome(str.substring(1, str.length() - 1));
} else {
return false;
}
}
// Driver code
public static void main(String[] args) {
// scanner object used to take user inputs
Scanner sc = new Scanner(System.in);
String str;
while(true){
System.out.println("Please enter a string to test for palindrome or type QUIT to exit:");
// taking a string and convert it into lower case
str = sc.nextLine().toLowerCase();
if (str.equals("quit")) {
break;
}
// otherwise
else{
// if method returns true
if (isPalindrome(str)) {
System.out.println("The input is a palindrome.");
}
// otherwise
else {
System.out.println("The input is not a palindrome.");
}
}
}
}
}
Chapter 16. PC #5. Palindrome Detector (page 1073)
A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes:
Able was I, ere I saw Elba
A man, a plan, a canal, Panama
Desserts, I stressed
Kayak
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 and backward. Demonstrate the method in a program.
The program should ask the user to enter a string, which is checked for palindrome property. The program displays whether the given input is a palindrome or not, then prompts the user to enter another string. If the user enters QUIT (case insensitive, then exit the program).
Test Case 1
Please enter a string to test for palindrome or type QUIT to exit:\n
Desserts, I stressedENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
KayakENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Desserts, I stressedENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
KayakENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 2
Please enter a string to test for palindrome or type QUIT to exit:\n
dadENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
dadENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 3
Please enter a string to test for palindrome or type QUIT to exit:\n
momENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
QuitENTER
momENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
QuitENTER
Test Case 4
Please enter a string to test for palindrome or type QUIT to exit:\n
helloENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quiTENTER
helloENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quiTENTER
Test Case 5
Please enter a string to test for palindrome or type QUIT to exit:\n
b,a,dENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
b,a,dENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 6
Please enter a string to test for palindrome or type QUIT to exit:\n
Able was I, ere I saw ElbaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Able was I, ere I saw ElbaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 7
Please enter a string to test for palindrome or type QUIT to exit:\n
A man, a plan, a canal, PanamaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
A man, a plan, a canal, PanamaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 8
Passed!
Please enter a string to test for palindrome or type QUIT to exit:\n
@abENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
ab@ENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
@aaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
aa@ENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
@abENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
ab@ENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
@aaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
aa@ENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Test Case 9
Passed!
Please enter a string to test for palindrome or type QUIT to exit:\n
abbaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
abcaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
aabcaaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
abaceedabaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
abbaENTER
The input is a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
abcaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
aabcaaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
abaceedabaENTER
The input is not a palindrome.\n
Please enter a string to test for palindrome or type QUIT to exit:\n
quitENTER
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education