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

Problem1. (20 points) Recursive Coding Problem. Using the Problem Solving Proces

ID: 3587426 • Letter: P

Question

Problem1. (20 points) Recursive Coding Problem. Using the Problem Solving Process. 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 return (output) the sum as follows: 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. am This application will calculate the solution for the following function: Please enter an integer: 4 x= 1 + 1/2 + 1/3-1/4-2.083. Constraint: Must implement recursively. 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: 8 8 *N 9N

Explanation / Answer

import java.util.*;

//Class RecursionSeriesPattern definition

public class RecursionSeriesPattern

{

//Function to calculate and return sum of the series up to n

double sumSeries(double n)

{

//Checks if n value is one return one

if(n == 1)

return 1;

//Otherwise

else

//Recursively call the function by deducting one from n and add it to 1/n value

return (1.0/n) + sumSeries(n-1);

}//End of function

//Function to print pattern

void printPattern(int no)

{

//Calls method to print 1 to n triangle

printTriangle(no);

//Calls method to print n to 1 triangle

printReverseTriangle(no);

}//End of function

//Method to print 1 to n triangle

public String printTriangle (int count)

{

//Checks if count is less than or equal to zero return null

if( count <= 0 )

return "";

//Add the return string with the p and recursively call the method by subtracting one from the count

String p = printTriangle(count - 1);

//Adds a star to p

p = p + "*";

//Displays p value

System.out.println(p);

//Returns p value

return p;

}//End of function

//Method to print n to 1 triangle

public String printReverseTriangle (int count)

{

//Checks if count is less than or equal to zero return null

if( count <= 0 )

return "";

//Creates a string and initializes it to null

String p = "";

//Loops till count

for (int i = 0; i < count; i++)

//Add star to the string p

p = p + "*";

//Add new line character to p

p += " ";

//Displays p

System.out.print(p);

//Add the return string with the p and recursively call the method by subtracting one from the count

return p + printReverseTriangle(count - 1);

}//End of function

//Main method definition

public static void main(String[] args)

{

//To store user entered number

int no;

//Scanner class object created to accept data from console

Scanner sc = new Scanner(System.in);

//Creates RecursionSeriesPattern class object

RecursionSeriesPattern rsp = new RecursionSeriesPattern();

//Accepts n value from the user

System.out.println("Enter the N value: ");

no = sc.nextInt();

//Calls the method and displays the result of the series

System.out.printf("Sum of the series: %.3f", rsp.sumSeries(no));

//Accepts n value from the user

System.out.println(" Enter the N value: ");

no = sc.nextInt();

//Calls the method and display the triangle pattern

rsp.printPattern(4);

}//End of main method

}//End of class

Sample Run:

Enter the N value: 4
Sum of the series: 2.083
Enter the N value: 6
*
**
***
****
*****
******
******
*****
****
***
**
*

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