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

A Palindrome is a word (or a number) that reads the same backwards. Write a prog

ID: 3703771 • Letter: A

Question

A Palindrome is a word (or a number) that reads the same backwards. Write a program that repeatedly prompts a user for a positive integer number and tests whether the number is a palindrome. Your program should convert the integer value given by the user into an int array storing each digit of the number in the appropriate element of the array. It should then check to see whether the array is a palindrome. Your program should follow the guidelines given below.

1. Write a method called getSize( int num ) that takes an integer number as input argument and returns the number of digits in that number. For e.g., getSize( 234 ) should return 3 and getSize( 76 ) should return 2.

2. Write a method called fillArray( int num, int[] intArray ) that takes an integer number and an integer array argument and fills the array with the appropriate digits of the number. For e.g., calling the above method fillArray( 87432, intArray ) would set intArray[0] to the value 8, intArray[1] to 7, ..., and intArray[4] to 2.

3. Write a boolean method called isPalindrome( int[] intArray ) that takes an integer array argument and returns the value true if the array is a palindrome else returns false.

4. Finally, write the main method that prompts the user repeatedly for an integer number, uses the getSize method to find out the number of digits in that number, allocates the int array for that size, calls the method fillArray to populate the array with the digits of the number, calls the isPalindrome method to find out whether the array is a palindrome, and prints out the result. Your program should terminate when the user enters -99.

Notes:
1. In Java you can allocate an array using a variable for the size, i.e., int[] intArray = new int[size]; where size is an int variable.
2. You SHOULD NOT use any methods from either the String or the Array class in your program.

You should only use JOptionPane for all the input/output.

Explanation / Answer

// java code to copy

/* package whatever; // don't place package name! */

import java.util.*;

import java.lang.*;

import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */

class Palindrome

{

public static int getSize(int num){

int size = 0;

while (num>0){

size++;

num=num/10;

}

return size;

}

public static void fillArray(int num,int[] array){

for (int j=array.length-1;j>=0;j--){

array[j] = num%10;

num=num/10;

}

}

public static boolean isPalindrome(int[] array){

int low = 0;

int high = array.length-1;

boolean isPal = true;

while (low<high){

if (array[low]==array[high]){

low++;

high--;

continue;

}

else{

isPal = false;

break;

}

}

return isPal;

}

public static void main (String[] args)

{

Scanner sc = new Scanner(System.in);

int num;

System.out.print("Enter the number: ");

num = sc.nextInt();

while (num!=-99){

int size = getSize(num);

int[] array = new int[size];

fillArray(num,array);

boolean isPal = isPalindrome(array);

if (isPal){

System.out.println("The given number "+num+" is palindrome");

}

else{

System.out.println("The given number "+num+" is not palindrome");

}

System.out.println();

System.out.print("Enter the number: ");

num = sc.nextInt();

}

}

}

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