Please explain each line of code of what the code is doing, thank you :-) (The c
ID: 3885379 • Letter: P
Question
Please explain each line of code of what the code is doing, thank you :-)
(The code in text format:)
public class RecursivePascal extends ErrorPascal {
public RecursivePascal() { super(); }
public RecursivePascal(boolean upsideDown) { super(upsideDown); }
/**
* Print a simple Pascal's triangle using recursion. Use upsideDown to toggle printing direction.
*
* @param n is the number of levels of the triangle
*/
public void printPascal(int n) {
validateParameters(n);
if (n == 0) {
System.out.format("%6d ",(binom(0, 0)));
} else {
if (upsideDown) {
for (int i = 0; i <= n; i++) {
System.out.format("%6d ", (binom(n, i)));
}
System.out.print(" ");
printPascal(n - 1);
} else {
printPascal(n - 1);
System.out.print(" ");
for (int i = 0; i <= n; i++) {
System.out.format("%6d ", (binom(n, i)));
}
}
}
}
/**
* Computes the binomial coefficient for a specific entry in Pascal's triangle.
*
* @param n is the row
* @param k is the column
* @return the binomial coefficient
*/
public int binom(int n, int k) {
validateParameters(n, k);
String binomKey = getCoeffHashKey(n, k);
if (coeffTable.containsKey(binomKey)) { return (Integer) coeffTable.get(binomKey); }
if (k == 0 || n == k) {
coeffTable.put(binomKey, 1);
return 1;
}
int coeff = binom(n - 1, k - 1) + binom(n - 1, k);
coeffTable.put(binomKey, coeff);
return coeff;
}
}
Explanation / Answer
The explaination of the code is as follows:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication4;
/**
*
* @author Rand0mb0t
*/
public class RecursivePascal extends ErrorPascal {
// Calling of super class's cnstructor ErrorPascal class's constructor
public RecursivePascal() { super(); }
// Parameterized constructor included callling for base class's constructor
public RecursivePascal(boolean upsideDown) { super(upsideDown); }
/**
* Print a simple Pascal's triangle using recursion. Use upsideDown to toggle printing direction.
*
* @param n is the number of levels of the triangle
*/
// function which actually prints the pascal reverse triangle
public void printPascal(int n) {
validateParameters(n); // For validating the value of n
if (n == 0) { //Conditions are checked ffor moving further to the binom function
System.out.format("%6d ",(binom(0, 0)));
} else {
if (upsideDown) {//if upsideDown then the pascal triangle is printed in reverse.
for (int i = 0; i <= n; i++) {
System.out.format("%6d ", (binom(n, i)));
}
System.out.print(" ");
printPascal(n - 1);
} else { // else pascal triangle is displayed
printPascal(n - 1);
System.out.print(" ");
for (int i = 0; i <= n; i++) {
System.out.format("%6d ", (binom(n, i)));
}
}
}
}
/**
* Computes the binomial coefficient for a specific entry in Pascal's triangle.
*
* @param n is the row
* @param k is the column
* @return the binomial coefficient
*/
public int binom(int n, int k) {
validateParameters(n, k);
//Getting Hash key value for the number n
String binomKey = getCoeffHashKey(n, k);
//Checking the condition where the coefficients and the key matches
if (coeffTable.containsKey(binomKey)) { return (Integer) coeffTable.get(binomKey); }
if (k == 0 || n == k) { // if it matches then the binomKey is given back with the keys
coeffTable.put(binomKey, 1);
return 1;
}
//coeff is calculated using the above operations and then the value is returned back to the coeffTable function.
int coeff = binom(n - 1, k - 1) + binom(n - 1, k);
coeffTable.put(binomKey, coeff);
return coeff;
}
}
Rate answer......Thankyou
Hope this helps....