My teacher wrote us a tester class for his problem with Pascal's triangle. Here it is:
public class TestTriangle {
public static void main(String args[]) {
PascalsTriangle triangle = new PascalsTriangle(7);
if (triangle.getElement(5, 2) == 10)
System.out.println("Triangle has passed one test.");
else
System.out.println("Triangle is wrong.");
triangle.showTriangle();
}
}
Here is my code:
public class PascalsTriangle {
private int rows;
private int column;
private int position;
/**
* Constructor for objects of class PascalsTriangle
*/
public PascalsTriangle() {
rows = 2;
}
public PascalsTriangle(int n) {
rows = n;
}
public int getElement(int n, int k) {
rows = n;
column = k;
//now the equation
int z;
int y;
int d;
for (z = 1; z <= n; z++) { //z is n! at nominator of equation
int a = z;
z = z * n;
z = a + z;
}
for (y = 1; y <= k; y++) { //y is k! at denominator of equation
int b = y;
y = y * k;
y = b + y;
}
int c = n - k;
for (d = 1; d <= c; d++) { //d is (n-k)! at denominator of equation
int e = d;
d = d * c;
d = e + d;
}
position = z / (y * d);
return position;
}
public showTriangle() { //outputs entire triangle
}
}
The only issue I have is with the showTriangle
method. I don't know how to make it output the entire triangle.
How would I print out the entire triangle if the only formula I have is for finding a specific position?