Question

In: Computer Science

(Palindrome integer) Write the methods with the following headers // Return the reversal of an integer,...

(Palindrome integer) Write the methods with the following headers
// Return the reversal of an integer, i.e., reverse(456) returns 654
public static int reverse(int number)
// Return true if number is a palindrome
public static boolean isPalindrome(int number)

Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome.

Here is a sample run: (red indicates a user input)

Enter a positive integer: 12321

12321 is a palindrome.

Continue? (y/n) y

Enter a positive integer: 12345

12345 is not a palindrome.

Continue? (y/n) n

Good bye!

This is for java if you could make it so it is debugged so theres no inputs that could make it crash! :(

Please leave comments

Solutions

Expert Solution

import java.util.Scanner;

public class PalindromeInteger {

    public static int reverseRecursive(int number) {
        int rev = 0;
        while (number > 0) {
            rev *= 10;
            rev += (number % 10);
            number /= 10;
        }
        return rev;
    }

    public static boolean isPalindrome(int number) {
        return reverseRecursive(number) == number;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char choice;
        do {
            System.out.print("Enter a positive integer: ");
            int n = in.nextInt();
            if (isPalindrome(n)) {
                System.out.println(n + " is a palindrome.");
            } else {
                System.out.println(n + " is not a palindrome.");
            }
            System.out.print("Continue? (y/n) ");
            choice = in.next().charAt(0);
        } while (choice == 'y' || choice == 'Y');
        System.out.println("Good bye!");
    }
}


Related Solutions

Part A: Create a project with a Program class and write the following two methods (headers...
Part A: Create a project with a Program class and write the following two methods (headers provided) as described below: A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range or a non-numeric...
Create a project with a Program class and write the following two methods (headers provided) as...
Create a project with a Program class and write the following two methods (headers provided) as described below: A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range or a non-numeric value was...
A palindrome is a string whose reversal is identical to the string. For example, 110010011 is...
A palindrome is a string whose reversal is identical to the string. For example, 110010011 is a palindrome. So are BOB and KAYAK. How many bit strings of length ?? are palindromes? Explain your solution clearly.
For this question you need to write some methods and class headers. (a) Assume that you...
For this question you need to write some methods and class headers. (a) Assume that you have written a Rectangle class with instance variables length and width. You have already written all set and get methods and area and perimeter methods. Write an equals() method that takes Object o as a parameter. The method should return true when the Object o is a rectangle with the same length and width. Answer: (b) A class named Fruit implements an interface called...
Write a method with the following header to return an array of integer values which are...
Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if the 2D array passed to the method is: 8 5 5 6 6 3 6 5 4 5 2 5 4 5 2 8 8 5 1 6 The method returns an array of integers which are the largest values from each row...
Challenge 4 – Random Integer in Range Write a function to return a random integer between...
Challenge 4 – Random Integer in Range Write a function to return a random integer between a minimum value and maximum value. var ival = IntRandomRange(, );
Write the following methods in a Java project: a) A Java method to determine and return...
Write the following methods in a Java project: a) A Java method to determine and return the sum of first three numbers, where three numbers are received as parameters. b) A Java method to determine and return the highest of N integers. The number of integers is received as a parameter. The method should prompt the user to enter the N numbers, then it return the highest. c) A Java method to determine and return an appropriate value indicating if...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT