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

Write a Java program that prints all 3 and 4-digit Palindrome non-negative integ

ID: 3688822 • Letter: W

Question

Write a Java program that prints all 3 and 4-digit Palindrome non-negative integers (non-negative means greater than or equal to zero).

Upload both your .java file and your output file to the DropBox. The output file may be either a text file made from monitor output or a screen shot(s) (you must show ALL output, so take several screen shots if needed) or you can put your screen shots inside a Word file.

For extra credit, print 10 numbers per line, and format it so that they are right-justified.

Background:

A numeric Palindrome is a non-negative integer that is the same number if you write its digits in reverse order. The following are Palindromes:

0, 1, 2, 3,4,5,6,7,8,9, 11, 22, 33, …, 99, 101, 111, 121, 131, …, 1001, …, 9999, 10001, …

See FunWIthDigits Ideal Output file for an example of what the output should look like for extra credit. If you are not going for extra credit, you may list them one per line or all on the same line with a tab or a couple of spaces in between.

Instructions:

Call your class FunWithDigits. The main method will be within this same class, so there will only be one file.

First, you’ll need a method called reverseDigits (I suggest using a static method) that will accept an integer and return an integer whose digits are in reverse order. One way to do this is find the right-most digit with the mod or remainder operator used with 10:

            rightMostDigit = number % 10;

Then find the number that’s left after you pull off the right-most digit. That can be found using integer division by 10;

            newNumber = number / 10;

Note that all these variables must be declared as integers.

Test that function by calling it from main and printing the results.

Note: If you absolutely cannot get that working, then just return the number that was passed in, and finish the rest of the assignment so that you don’t lose those other valuable points. In the meantime, contact me and I can look at what you’ve done and give you some advice.
Don’t wait until the last minute!

Once you have that working, then you will need a method called isPalindrome (again I would use a static method) that will accept an integer and return a boolean. This boolean is true if the number is a Palindrome and false if not. If the number is equal to the same number with its digits reversed, then it is indeed a Palindrome. Again, test it by calling it from the main method. Once that is working, then set up a for-loop in the main method whose counter goes from 0 to 10 (you’ll change this to 100 through 9999 later, but initially just do 0 to 10 since it’s easier to test). You’ll call isPalindrome and pass it the counter. If it returns true, print the counter. Get that working.

Then change the for-loop to go from 100 to 9999 (100 to 999 will cover all 3-digit numbers, and 1000-9999 will cover all 4-digit numbers).

Explanation / Answer

/**
* @author Srinivas Palli
*
*/
public class FunWithDigits {
   /**
   * method to reverse a number
   *
   * @param number
   * @return
   */
   public static int reverseDigits(int number) {

       int reverse = 0;
       while (number != 0) {
           reverse = (reverse * 10) + (number % 10);
           number = number / 10;
       }
       return reverse;
   }

   /**
   * check weather number is palindrom or not
   *
   * @param number
   * @return
   */
   public static boolean isPalindrome(int number) {

       if (reverseDigits(number) == number)
           return true;
       else
           return false;

   }

   /**
   * @param args
   */
   public static void main(String[] args) {

       try {
           int count = 0;
           for (int i = 0; i < 9999; i++)
               if (isPalindrome(i)) {

                   if (count % 10 == 0) {
                       System.out.println();

                   }
                   System.out.print(i + " ");
                   count++;
               }

       } catch (Exception e) {
           // TODO: handle exception
       }

   }

}

OUTPUT:


0 1 2 3 4 5 6 7 8 9
11 22 33 44 55 66 77 88 99 101
111 121 131 141 151 161 171 181 191 202
212 222 232 242 252 262 272 282 292 303
313 323 333 343 353 363 373 383 393 404
414 424 434 444 454 464 474 484 494 505
515 525 535 545 555 565 575 585 595 606
616 626 636 646 656 666 676 686 696 707
717 727 737 747 757 767 777 787 797 808
818 828 838 848 858 868 878 888 898 909
919 929 939 949 959 969 979 989 999 1001
1111 1221 1331 1441 1551 1661 1771 1881 1991 2002
2112 2222 2332 2442 2552 2662 2772 2882 2992 3003
3113 3223 3333 3443 3553 3663 3773 3883 3993 4004
4114 4224 4334 4444 4554 4664 4774 4884 4994 5005
5115 5225 5335 5445 5555 5665 5775 5885 5995 6006
6116 6226 6336 6446 6556 6666 6776 6886 6996 7007
7117 7227 7337 7447 7557 7667 7777 7887 7997 8008
8118 8228 8338 8448 8558 8668 8778 8888 8998 9009
9119 9229 9339 9449 9559 9669 9779 9889

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