Question
You will create three (3) static methods described below, and test each of them by calling them from main. The three are as follows: sumDigits
You will create three (3) static methods described below, and test each of them by calling them from main. The three are as follows:
sumDigits
reverse
isPalindrome
The descriptions of the three methods you are to create and test are as follows:
The sumDigits method
The signature is as follows:
public static int sumDigits(int n)
This method will compute the sum of the digits of an integer. For example:
sumDigits(234) should return the value 9, since 2 + 3 + 4 = 9.
Hints: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 (which is = 4). To remove 4 from 234, use 234/10 (which is = 23)
Use a loop to repeatedly extract and remove the digit until all the digits are extracted.
Write a program that prompts the user to enter an integer and displays the sum of all its digits.
The reverse method
The signature is as follows:
public static int reverse(int number)
This method will take an integer as an argument and reverse the digits, and return the value. For example,
reverse(456) will return 654.
Hints: One way (not the only way) to do this part is to take the original integer and convert it to a string, reverse the string, convert it back to an integer and then return the integer.
Remember:
Use Integer.toString(someString) to convert a String to an integer
Use Integer.parseInt(someInt) to convert an integer to a string
The isPalindrome Method
The signature is as follows:
public static boolean isPalindrome(int number)
This method will take an integer as an argument and return true if the integer is palindromic (the same forward as it is backwards), or return false if the integer is non-palindromic.
For example, isPalindrome(4554) would return true. However, isPalindrome(4567) would return false.
You must use the reverse method that you defined earlier in implementing isPalindrome. Remember that a palindrome is the same forward as backwards (reversed) so the reverse method will come in handy!
Special Notes
Make sure that you test all three of the aforementioned methods. Since isPalindrome calls reverse, you dont have to call reverse directly from main.
However, you must call sumDigits and isPalindrome from the main method in order to perform basic testing. You may request user input from the keyboard, and then return the sum of the digits in the integer the user enters, as well as whether or not the integer is a palindrome or not.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started