Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Using the Palindrome.java file and the PalindromeApp.java file, rewrite and c

ID: 3683591 • Letter: 1

Question

1. Using the Palindrome.java file and the PalindromeApp.java file, rewrite and correct the Palindrome.java file so that it correctly tests whether a string is a palindrome or not. Palindrome.java: public class Palindrome public static boolean test (String candidate) // Returns true if candidate is a palindrome, false otherwise char ch; int length; int numLetters; int charCount; // current candidate character being processed // length of candidate string // number of letter characters in candidate string // number of characters checked so far char fromQueue: boolean stillPalindrome: / true if string might still be a palindrome // current character dequeued from queue BoundedQueueInterfacecCharacter> queue; //holds non blank string characters // initialize variables and structures length = candidate, length() ; queue new ArrayBndQueue(length) numLetters -0 // obtain and handle characters for (int i = 0; i

Explanation / Answer

import java.util.*; public class Palindrome { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter a number: "); int num = scanner.nextInt(); System.out.println("Please enter a string: "); String str = scanner.next(); Palindrome palin = new Palindrome(); int revNum = palin.reverse(num); String revStr = palin.reverse(str); if (num == revNum) { System.out.printf(" The number %d is a Palindrome ", num); } else { System.out.printf(" The number %d is not a Palindrome ", num); } if (str.equalsIgnoreCase(revStr)) { System.out.printf(" The string '%s' is a Palindrome ", str); } else { System.out.printf(" The string '%s' is not a Palindrome ", str); } } public String reverse(String str) { StringBuilder revStr = new StringBuilder(); for (int i = str.length()-1; i >= 0; i--) { revStr.append(str.charAt(i)); } return revStr.toString(); } }