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

Write a program called Assg4.java to solve the following problem. Write a progra

ID: 3761188 • Letter: W

Question

Write a program called Assg4.java to solve the following problem.

Write a program to solve the following problem. Given a small integer (e.g. 78), we can reverse it (giving 87) and add the reverse to itself (giving 165). If this is performed repeatedly then usually a number that is a palindrome is eventually produced.

Write a program that takes an integer on the command line, checks that it is not negative or bigger than 200 (in which case a suitable message should be printed and no computation performed) and then prints it out followed by every number that is produced during repeatedly performing the above operation until a palindromic number is produced. A typical run should be as follows.

% java Assg4 78
78
165
726
1353
4884
The number 78 took 4 steps to reach a palindrome.

In addition, your program should terminate with an error message if more than 10 steps are taken, as follows.

% java Assg4 89
89
187
968
1837
9218
17347
91718
173437
907808
1716517
8872688
The number 89 takes more than 10 steps to reach a palindrome.

[Note that you may safely ignore the possibility of integer overflow in this assignment, given the size and iteration limits imposed above.]

Hint: One way to proceed is to write static methods to convert an int to a String and vice-versa first, and test them. Then write a static method to test whether or not a String is a palindrome, and test it. Now use these methods to help solve the problem.

Explanation / Answer

/**The java program that takes the command line argument arg[0]
* as integer value and prints until the palindrome number is found.
* Then print the number iterations it takes to complete to find
* the palindrome number*/
//Assg4.java
public class Assg4
{
   public static void main(String[] args)
   {

       //Convert string integer to integer value
       int number=Integer.parseInt(args[0]);
       int tempNumber=number;
       int rev;
       int sum;
       boolean repeat=true;
       int count=1;

       System.out.println(number);
       //repeat the loop until the palindrom number is found
       while(repeat)
       {
          
           //reverse the number
           rev=reverse(number);
           //add the number and its reverse
           sum=add(number,rev);
          
           //print sum value
           System.out.println(sum);
          
           //check if sum is palindrome
           if(isPalindrome(sum))
               repeat=false;
           else
           {
               //increment the count
               count++;          
               //set sum to number for next iteration
               number=sum;
           }

       }

       //print number of iterations takes to find plaindrome number
       System.out.println("The number "+tempNumber+" takes more than "+count
               +" steps to reach a palindrome");

   }

   //Method that returns true if the given number is palindrome
   private static boolean isPalindrome(int sum)
   {
       //convert the number sum to stringBuffer object
       StringBuffer strSum = new StringBuffer(String.valueOf(sum));
       //convert the strSum to its reverse
       StringBuffer reverse = new StringBuffer(strSum);
       //call reverse on strSum
       strSum.reverse();

       //check if sum and reverse are equal
       return strSum.toString().equals(reverse.toString());
   }

   //Method returns the sum of two numbers
   private static int add(int number, int reverse)
   {
       return number+reverse;      
   }

   //Method that reverse the number
   private static int reverse(int number)
   {
       int rev=0;
       while( number != 0 )
       {
           rev = rev * 10;
           rev = rev + number%10;
           number = number/10;
       }
       return rev;
   }
}

-------------------------

Sample Output:

java Assg4 78

78
165
726
1353
4884
The number 78 takes more than 4 steps to reach a palindrome

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