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

Activity 2. Building Number Palindromes. In this activity, you will design a met

ID: 3669755 • Letter: A

Question

Activity 2. Building Number Palindromes. In this activity, you will design a method that builds number palindromes: Although some numbers are palindromes (e.g., all 1-digit numbers, 11, 242, etc.), not all numbers are palindromes. Those that are not palindromes can be systematically modified to become a palindrome. How does this work?

Example: 57 is not a palindrome.

Let’s add to 57 its “reverse”, which is 75 (number 57 read from right to left):

57 + 75 = 132

This is not a palindrome, so let’s keep going.

Let’s add to 132 its reverse, which is 231:

132 + 231 = 363

This is a palindrome.

We were able by following a systematic approach (current number + its reverse) to build a palindrome from 57 in 2 steps.

In this activity, we want to know how many steps it takes for a number to become a palindrome.

Building Number Palindromes

Your method is as follows:

Name: buildingNumberPalindrome;

Parameter: an integer – the number from which we try to build a palindrome;

Returns: an integer – the number of steps it takes to build a palindrome from the integer that was passed to the method.

What you have to do it the following:

Given a number (an integer), let’s call it num:

1/ Check if num is a palindrome (you will design a method isPalindrome to do this for you).

2/ If num is a palindrome: print “it is a palindrome” and return 0 as the number of steps it took you to transform num into a palindrome.

3/ If num is not a palindrome, then create a variable builtPalindrome that contains num and a variable numberOfSteps set to 0. Then keep doing the following until either you have reached the maximum allowed number of steps (20) or you have built a palindrome:

Add to builtPalindrome the reverse of builtPalindrome (you will design a method BuiltPalindrome to do this for you).

Increase numberOfSteps by 1.

Check if the resulting builtPalindrome number is a palindrome.

If it is a palindrome then this property will cause you to stop the repetition.

Otherwise keep repeating (provided that you have not yet reached 20 steps).

[Once you are done repeating:]

4/ If you were able to build a palindrome: print “it took numberOfSteps steps to make num a palindrome” and return numberOfSteps.

5/ If you were not able to build a palindrome (which means that you have reached the maximum number of steps), print “it would take more than 20 steps to transform num into a palindrome” and return -1.

How to use buildNumberPalindrome?

From within the main (where it says Activity 2), here is what you have to implement:

1/ Ask the user for his/her name

2/ Ask the user, using his/her name, whether or not s/he wants to build number palindromes

3/ If the user wants to do it and if this user has not already used this method 4 times (you need to keep track of the number of times the user has built palindromes and keep asking until this number has been reached or the user decides not to build palindromes anymore), then:

Ask the user the number s/he wants to build number palindromes from; and then

Call the buildNumberPalindrome method on this number.

Keep asking if the user wants to keep using it.

Otherwise, print “Thank you! Good bye!”.

In addition to method buildNumberPalindrome, you have to design two “helper” methods: isPalindrome and reverse. We describe each of them below:

isPalindrome

Your method is as follows:

Name: isPalindrome

Parameter: an integer, let’s call it num

Returns: a boolean – true if num is a palindrome, false otherwise

reverse

Your method is as follows:

Name: reverse

Parameter: an integer, let’s call it num

Examples: reverse(87) is 78

                     reverse (54) is 45

                     reverse (8) is 8

Note: A lot of code is already given to you. Please keep it as is and implement what is missing, where prompted.

Requirements:

1/ The method buildNumberPalindrome should be implemented using a while loop.

2/ In the method isPalindrome, the missing code should be implemented using the method numDigit (given to you) and a for loop.

3/ In the method reverse, the missing code should be implemented using the method numDigit (given to you) and using a for loop.

4/ In the main method in the area reserved for Activity 2, use a while loop to keep asking the user if s/he wants to keep using the buildNumberPalindrome method.

3/ Some code has been prepared for you, so you just have to complete it without modifying any of the code that is already in the file Lab5Spring16.java and do make sure that you write your code where prompted (carefully read all comments in the file).

Instructions:

Write the pseudocode of your algorithm in the docx file named “yourLastName-yourFirstName-lab5.docx” that you started for Activity 1.

Modify the code provided in Lab5Spring16.java where prompted (and only where prompted) to make sure that the code follows the above instructions.

Note: inability to follow instructions will be penalized by 10 points.

Now, here is what you have to turn in:

The modified java file called Lab5Spring16.java.

The docx file in which you have described your pseudocode (= your algorithm).

Building Number Palindromes

Your method is as follows:

Name: buildingNumberPalindrome;

Parameter: an integer – the number from which we try to build a palindrome;

Returns: an integer – the number of steps it takes to build a palindrome from the integer that was passed to the method.

What you have to do it the following:

Given a number (an integer), let’s call it num:

1/ Check if num is a palindrome (you will design a method isPalindrome to do this for you).

2/ If num is a palindrome: print “it is a palindrome” and return 0 as the number of steps it took you to transform num into a palindrome.

3/ If num is not a palindrome, then create a variable builtPalindrome that contains num and a variable numberOfSteps set to 0. Then keep doing the following until either you have reached the maximum allowed number of steps (20) or you have built a palindrome:

Add to builtPalindrome the reverse of builtPalindrome (you will design a method BuiltPalindrome to do this for you).

Increase numberOfSteps by 1.

Check if the resulting builtPalindrome number is a palindrome.

If it is a palindrome then this property will cause you to stop the repetition.

Otherwise keep repeating (provided that you have not yet reached 20 steps).

[Once you are done repeating:]

4/ If you were able to build a palindrome: print “it took numberOfSteps steps to make num a palindrome” and return numberOfSteps.

5/ If you were not able to build a palindrome (which means that you have reached the maximum number of steps), print “it would take more than 20 steps to transform num into a palindrome” and return -1.

How to use buildNumberPalindrome?

From within the main (where it says Activity 2), here is what you have to implement:

1/ Ask the user for his/her name

2/ Ask the user, using his/her name, whether or not s/he wants to build number palindromes

3/ If the user wants to do it and if this user has not already used this method 4 times (you need to keep track of the number of times the user has built palindromes and keep asking until this number has been reached or the user decides not to build palindromes anymore), then:

Ask the user the number s/he wants to build number palindromes from; and then

Call the buildNumberPalindrome method on this number.

Keep asking if the user wants to keep using it.

Otherwise, print “Thank you! Good bye!”.

Explanation / Answer

Solution:

package com.nancy.chegg;

import java.util.Scanner;

public class Lab5Spring16 {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);
       String name;
       System.out.println("Please enter your name : ");
       name = scan.next();
       int userLimit = 0;
       do {
           System.out.println("Hi " + name
                   + "! Do you want to build palindrome number ?(y/n) ");
           String res = scan.next();
           if (res.equalsIgnoreCase("y")) {
               System.out.println(
                       "Please enter the number to make palindromes : ");
               int numSteps = buildingNumberPalindrome(scan.nextInt());
               System.out
                       .println("the number of steps requireed = " + numSteps);
               userLimit++;
           } else {
               System.out.println("Thank you! Good bye!");
               break;
           }
       } while (userLimit <= 4);
   }

   public static int buildingNumberPalindrome(int num) {
       boolean isPalin = false;
       int numberOfSteps = 0;
       int builtPalindrome = 0;
       while (!isPalin && numberOfSteps <= 20) {
           isPalin = isPalindrome(num);
           if (isPalin) {
               System.out.println("It is a palindrome");
               return numberOfSteps;
           } else {
               builtPalindrome = num;
               num = BuiltPalindrome(builtPalindrome);
               numberOfSteps++;
           }
       }

       return 0;
   }

   private static int BuiltPalindrome(int builtPalindrome) {
       int revBuiltPalindrome = reverse(builtPalindrome);
       return builtPalindrome + revBuiltPalindrome;
   }

   private static boolean isPalindrome(int num) {

       int palindrome = num;
       int reverse = 0;

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

       // when original and reverse of number is equal than number is
       // palindrome.
       if (num == reverse) {
           return true;
       }
       return false;
   }

   private static int reverse(int num) {
       int revNum = 0;
       while (num != 0) {
           revNum = revNum * 10;
           revNum = revNum + num % 10;
           num = num / 10;
       }
       return revNum;
   }
}

Sample Run :

Please enter your name : Nancy
Hi Nancy! Do you want to build palindrome number ?(y/n) y
Please enter the number to make palindromes : 57
It is a palindrome
the number of steps requireed = 2
Hi Nancy! Do you want to build palindrome number ?(y/n)

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