5#Write a JAVA method from the client perspective to test whether a String is a
ID: 3759304 • Letter: 5
Question
5#Write a JAVA method from the client perspective to test whether a String is a palindrome using a deque.
The method header is: public boolean isPalindrome(String s)
(*The method should not be recursive.*)
Put the letters of the String into a DequeInterface object.
Then use only the methods of DequeInterface to determine if the String is a palindrome.
****this is the driver******
public class HomeworkW11Driver {
public static void main(String[] args) {
// Q5 palindromes
System.out.println("Q7");
String p = "anna";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "ana";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "a";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "amanaplanacanalpanama";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "ababb";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
p = "ab";
System.out.println(p + " is a palindrome? " + isPalindrome(p));
System.out.println();
// Q6 splice client
QueueInterface q1 = new LinkedQueue();
q1.enqueue(4);
q1.enqueue(8);
q1.enqueue(9);
q1.enqueue(2);
QueueInterface q2 = new LinkedQueue();
q2.enqueue(1);
q2.enqueue(3);
q2.enqueue(7);
q2.enqueue(6);
splice(q1, q2);
System.out.println("q1 contains: 4 8 9 2 1 3 7 6");
((LinkedQueue)q1).display();
System.out.println("q2 still contains: 1 3 7 6");
((LinkedQueue)q2).display();
// Q7 splice ArrayQueue
ArrayQueue aq1 = new ArrayQueue();
aq1.enqueue(4);
aq1.enqueue(8);
aq1.enqueue(9);
aq1.enqueue(2);
ArrayQueue aq2 = new ArrayQueue();
aq2.enqueue(1);
aq2.enqueue(3);
aq2.enqueue(7);
aq2.enqueue(6);
aq1.splice(aq2);
System.out.println("q1 contains: 4 8 9 2 1 3 7 6");
aq1.display();
System.out.println("q2 still contains: 1 3 7 6");
aq2.display();
// Q8 splice LinkedQueue
LinkedQueue lq1 = new LinkedQueue();
lq1.enqueue(4);
lq1.enqueue(8);
lq1.enqueue(9);
lq1.enqueue(2);
LinkedQueue lq2 = new LinkedQueue();
lq2.enqueue(1);
lq2.enqueue(3);
lq2.enqueue(7);
lq2.enqueue(6);
lq1.splice(lq2);
System.out.println("q1 contains: 4 8 9 2 1 3 7 6");
lq1.display();
System.out.println("q2 still contains: 1 3 7 6");
lq2.display();
}
public static boolean isPalindrome(String s) {
// YOUR CODE HERE!
return false;
}
public static void splice(QueueInterface firstQueue, QueueInterface secondQueue) {
// YOUR CODE HERE!
}
}
Explanation / Answer
public boolean isPalindrome(String testString) { StringBuffer sb = new StringBuffer(testString); String reverseString = sb.reverse().toString(); if(testString.equalsIgnoreCase(reverseString)) { return true; else { return false; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.