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

A palindrome is a sequence of characters that reads the same backward as forward

ID: 3552283 • Letter: A

Question

A palindrome is a sequence of characters that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554, and 11611. Write an application called Palindrome.java that asks the user to enter in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long, display an error message dialog indicating the problem to the user. When the user dismisses the error dialog, allow the user to enter a new value. Your program will have the following four methods main() method, which controls the execution of the program; retrieveInput() method, which prompts and retrieves the input values; check() method, which determines whether it is a palindrome; and display() method, which displays the result.

Explanation / Answer

A palindrome is a sequence of characters that reads the same backward as forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write an application that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long, display an error message and allow the user to enter a new value.
Sample Output


Program Template
1 // Lab 4: Palindrome.java
2 // Program tests for a palindrome
3 import java.util.Scanner;
45
public class Palindrome
6 {
7 // checks if a 5-digit number is a palindrome
8 public void checkPalindrome()
9 {
10 Scanner input = new Scanner( System.in );
11
12 int number; // user input number
13 int digit1; // first digit
14 int digit2; // second digit
15 int digit4; // fourth digit
16 int digit5; // fifth digit
17 int digits; // number of digits in input
18
19 number = 0;
20 digits = 0;
21
22 /* Write code that inputs a five-digit number. Display an error message
23 if the number is not five digits. Loop until a valid input is received. */
24
25 /* Write code that separates the digits in the five digit number. Use
26 division to isolate the left-most digit in the number, use a remainder
27 calculation to remove that digit from the number. Then repeat this
28 process. */
29
30 /* Write code that determines whether the first and last digits are
31 identical and the second and Fourth digits are identical. Output
32 whether or not the original string is a palindrome. */
33
34 } // end method checkPalindrome
35 } // end class Palindrome

1 // Lab 4: PalindromeTest.java
2 // Test application for class Palindrome
3 public class PalindromeTest
4 {
5 public static void main( String args[] )
6 {
7 Palindrome application = new Palindrome();
8 application.checkPalindrome();
9 } // end main
10 } // end class PalindromeTest

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote