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

Write a Java application that implements these two functions using recursive met

ID: 640772 • Letter: W

Question

Write a Java application that implements these two functions using recursive methods.

a. First Recursive Function. When one passes an integer as input (n for example), the return should retun the sum as follows:

            1 + 1/2 + 1/3 + 1/4 + ... 1/(n-1) + 1/n

Example:
If the function's argument is 4, the function should return the value of 2.083 which is the sum of 1 + 1/2 + 1/3 + 1/4.

Sample Output:
Please enter an integer: 4
x = 1 + 1/2 + 1/3 + 1/4 = 2.083.

b. Second Recursive Function. The second function when you pass it an integer (n for example) is supposed to print lines of asterisks in the following format:

*
**
***
****
****
***
**
*

Sample Output:
Please enter an integer (1-10) an I will print a recursive Bar Chart: 3
*
**
***
***
**
*

Explanation / Answer

import java.util.Scanner;


public class RecFunctions {
  
   public static double fn1(int n){
       if(n == 1){
           return 1;
       }
       else{
           return (1.0 / n) + fn1(n - 1);
       }
   }
  
   public static void fn2(int n, int c){
       if(n == 0){
           return;
       }
       else{
           for(int i = n - 1; i < c; ++i){
               System.out.print("*");
           }
           System.out.println();
          
           fn2(n - 1, c);
          
           for(int i = n - 1; i < c; ++i){
               System.out.print("*");
           }
           System.out.println();
       }
   }
  
   public static void fn2(int n){
       fn2(n, n);
   }
  
   public static void main(String args[]){
       Scanner in = new Scanner(System.in);
       System.out.print("Enter value of n for function One: ");
       int n = in.nextInt();
       System.out.printf("Result of function 1 is %.3f ", fn1(n));
       System.out.print("Enter value of n for function Two: ");
       n = in.nextInt();
       System.out.println("Executing Function Two ");
       fn2(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