Background
Recursion is generally considered a challenging topic to wrap your head around, but very useful and simple to do once you get into the correct mindset. Recursive problems are easy to spot once you can think in a recursive manner. For example, in math: 5! = 5 * 4 * 3 * 2 * 1, or 5 * 4!, or 5 * (5 - 1)!. Which means that n! = n * (n - 1)!. So 5! = 5 * 4!, and 4! = 4 * 3!.. This continues until you get to 1!, which is just 1. As a quick exercise, try to identify the base case and smaller caller for a factorial method in java.
Exercise
For each of the five problems presented inProject5.java, fill in the recursive methods so that they compute the correct value. The main method currently calls each of the recursive methods with one test case. Make sure to test your methods with multiple test cases to make sure they all work properly.
You will not need to use loops at all. Any method using loops will not receive credit. You will receive 1.25 points for each correct recursive method, totaling 5 points for the project with the possibility of 0.5 points of bonus for completing problem #5.
REQUIRED: Given a number of rows, return the number of stars needed to make a triangle of that many rows. For example, 4 rows would yield 10 stars:
*
* *
* * *
* * * *
REQUIRED: Given a positive integer, return the sum of the digits in that integer. Therefore, 945 would return 9 + 4 + 5, or 18. You CANNOT convert the int into a String and then back. You will need to use modulus (%) and integer division (/) to achieve this.
REQUIRED: Given a positive integer, count the number of 7s that appear in the number. Again, you cannot use Strings. You will likely need to use a similar solution as #2. Ex: 8675309 -> 1 (one seven in this number)
REQUIRED: Given two numbers, base and exponent, return baseexponent.
BONUS (0.5 pts): Given a String, return whether or not the String is a palindrome. Make sure to test with other words besides the given one!
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here