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...

1 answer below »

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 in
Project5.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.




  1. 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:





*



* *



* * *



* * * *






  1. 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.








  1. 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)








  1. REQUIRED: Given two numbers, base and exponent, return baseexponent.








  1. 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!



Answered Same DayApr 18, 2021

Answer To: Background Recursion is generally considered a challenging topic to wrap your head around, but very...

Neha answered on Apr 21 2021
151 Votes
import java.io.*;
public class Project5
{
    public static void main( String[] args )
    {
        // tri
angle stars -- how many stars will be in a triangle of _ rows?
        // e.g. 3 rows is 6 stars
        // *
        // * *
        // * * *
int x = 5;
        System.out.format("A triangle with %d rows contains %d stars\n", x, triStars(x));
        // sum of digits -- what is the sum of the digits in a given number?
        // e.g. sum of 548 is 5 + 4 + 8 = 17.
        // hint: you will need to use modulus and integer division to pull this one off
        // you can NOT convert the int to a String!
        int number = 12345;
        System.out.format("The sum of the digits in the number %d is %d\n", number, sumDigits( number ) );
        // count 7s -- how many 7s appear in the number?
        // e.g. 4726787 is three 7s
        number = 713274772;
        System.out.format("There are %d occurrences...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here