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

Please explain what these two kodes do, and line-by-line what is happening in th

ID: 3885324 • Letter: P

Question

Please explain what these two kodes do, and line-by-line what is happening in the code

package recursion: import apiDesign.ErrorPascal: import apiDesign.Pascal: import java.lang.*: public class RecursivePascal extends ErrorPascal implements Pascal { public void printPascal(int n, boolean upsideDown) { ErrorPascal(n): if (upsideDown) { for (int i = 0: i 0) printPascal (n - 1, upsideDown): } else { if (n > 0) printPascal (n - 1, upsideDown): for (int i = 0: i 0) printPascal (n - 1, upsideDown): for (int i = 0: i = 0: j--) { System.out.println(row[j] + " "): } } system.out.println(): } } public int binom(int n, int k) { if (n == k || k == 0) return 1: if (n

Explanation / Answer

Explaining ReursivePascal class:

The first line of class declaration tells us that this class implements the Pascal interface and extends the ErrorPascal class.

The method printPascal takes a number n, and one boolean as upsideDown.
If n = 3,
then with upsideDown as true, method calls will be in following order:
-> Print binom(n,i) values from i=0 to i=3
-> Call printPascal method again with n=2
-> Print binom(n,i) values from i=0 to i=2
-> Call printPascal method again with n=1
-> Print binom(n,i) values from i=0 to i=1
-> Call printPascal method again with n=0
-> Print binom(n,i) values for i=0
-> as n is equal to 0 now, the execution stops..

While with same n=3, if upsideDown would have been false; method calls will be in following order:
-> Call printPascal method again with n=2
-> Call printPascal method again with n=1
-> Call printPascal method again with n=0
-> As n=0, now no need to call further,
-> Print binom(n,i) values for i=0
-> Print binom(n,i) values from i=0 to i=1
-> Print binom(n,i) values from i=0 to i=2
-> Print binom(n,i) values from i=0 to i=3
-> the execution stops..


Method binom calculates the binomial expression as (n)C(k), the expression (n)C(k) is defined as (n-1)C(k-1) + (n-1)C(k). So this function basically breaks the expression into smaller problms recursively.

=================================================================

Explaining ReursivePascal2 class:

The first line of class declaration tells us that this class implements the Pascal interface and extends the ErrorPascal class.

Method binom here is also same as explained above.


The method printPascal takes a number n, and one boolean as upsideDown.
If n = 3,
then with upsideDown as true,
We create an array of n/2 + 1 elements named row.. it would be of size 2 in this case..
This array keep the values of binom(n, i) at row[i] index..

So with upsideDown as true
-> We first fill the row array and print values of binom(n,i) for i = 0 to i=n/2+1
-> then depending upon, whether n is odd or even, we print the complete row array starting from rightmost value to leftmost value
-> if n is greater than 0, we recursively call this method with n-1 value