Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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.");
}
}
}
}
}
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
[画像:Test Case 1 Failed Show what's missing Please enter a string to test for palindrome or type QUIT to exit: \n Desserts, I stressed ENTER The input is not a palindrome. \n Please enter a string to test for palindrome or type QUIT to exit: \n Kayak ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Test Case 2 Passed! Please enter a string to test for palindrome or type QUIT to exit: \n dad ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Test Case 3 Passed! Please enter a string to test for palindrome or type QUIT to exit: \n mom ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n Quit ENTER]
expand button
Transcribed Image Text:Test Case 1 Failed Show what's missing Please enter a string to test for palindrome or type QUIT to exit: \n Desserts, I stressed ENTER The input is not a palindrome. \n Please enter a string to test for palindrome or type QUIT to exit: \n Kayak ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Test Case 2 Passed! Please enter a string to test for palindrome or type QUIT to exit: \n dad ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Test Case 3 Passed! Please enter a string to test for palindrome or type QUIT to exit: \n mom ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n Quit ENTER
[画像:Test Case 4 Passed! Please enter a string to test for palindrome or type QUIT to exit:\n hello ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quiT ENTER Test Case 5 Passed! Please enter a string to test for palindrome or type QUIT to exit: \n b,a,d ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Test Case 6 Failed Show what's missing Please enter a string to test for palindrome or type QUIT to exit: \n Able was I, ere I saw Elba ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quit ENTER Test Case 7 Failed Show what's missing Please enter a string to test for palindrome or type QUIT to exit: \n A man, a plan, a canal, Panama ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Test Case 8 Failed Show what's missing 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 abe ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n @aa ENTER The inn The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n aa@ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quit ENTER Test Case 9 Passed! Please enter a string to test for palindrome or type QUIT to exit: \n abba ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n abca|ENTER| The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n aabcaa ENTER CanadENT The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n abaceedaba ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Screenshot]
expand button
Transcribed Image Text:Test Case 4 Passed! Please enter a string to test for palindrome or type QUIT to exit:\n hello ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quiT ENTER Test Case 5 Passed! Please enter a string to test for palindrome or type QUIT to exit: \n b,a,d ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Test Case 6 Failed Show what's missing Please enter a string to test for palindrome or type QUIT to exit: \n Able was I, ere I saw Elba ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quit ENTER Test Case 7 Failed Show what's missing Please enter a string to test for palindrome or type QUIT to exit: \n A man, a plan, a canal, Panama ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Test Case 8 Failed Show what's missing 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 abe ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n @aa ENTER The inn The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n aa@ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n quit ENTER Test Case 9 Passed! Please enter a string to test for palindrome or type QUIT to exit: \n abba ENTER The input is a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit:\n abca|ENTER| The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n aabcaa ENTER CanadENT The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n abaceedaba ENTER The input is not a palindrome.\n Please enter a string to test for palindrome or type QUIT to exit: \n quit ENTER Screenshot
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
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.
Similar questions
    SEE MORE QUESTIONS
    Recommended textbooks for you
    Text book image
    Database System Concepts
    Computer Science
    ISBN:9780078022159
    Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
    Publisher:McGraw-Hill Education
    Text book image
    Starting Out with Python (4th Edition)
    Computer Science
    ISBN:9780134444321
    Author:Tony Gaddis
    Publisher:PEARSON
    Text book image
    Digital Fundamentals (11th Edition)
    Computer Science
    ISBN:9780132737968
    Author:Thomas L. Floyd
    Publisher:PEARSON
    Text book image
    C How to Program (8th Edition)
    Computer Science
    ISBN:9780133976892
    Author:Paul J. Deitel, Harvey Deitel
    Publisher:PEARSON
    Text book image
    Database Systems: Design, Implementation, & Manag...
    Computer Science
    ISBN:9781337627900
    Author:Carlos Coronel, Steven Morris
    Publisher:Cengage Learning
    Text book image
    Programmable Logic Controllers
    Computer Science
    ISBN:9780073373843
    Author:Frank D. Petruzella
    Publisher:McGraw-Hill Education