Question: I need to write this program using Java. I have a method called numDigit complete: public static int numDigits(int num) { int copyOfNum = num;
I need to write this program using Java. I have a method called numDigit complete:
public static int numDigits(int num) { int copyOfNum = num; int digits = 1; copyOfNum = copyOfNum / 10; while (copyOfNum !=0) { digits++; copyOfNum = copyOfNum / 10; } return digits; }
Activity 1. 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.
Lets add to 57 its reverse, which is 75 (number 57 read from right to left):
57 + 75 = 132
This is not a palindrome, so lets keep going.
Lets 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 is the following: Given a number (an integer), lets 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, 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, lets 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, lets call it num Returns: an integer num read from right to left.Examples: reverse (87) is 78 reverse (54) is 45 reverse (8) is 8 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
