Notes to PascalTriangle
You are to write a class PascalTriangle which prints out the first
N lines of the pascal triangle. For example, if N=5,
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
The entries in the triangle are computed by the method
static int choose(int n, int k) { }
which you are to write. The rows and columns
of the triangle begin with index 0. The method choose is
recursive, with recursive definition,
{ 1 if n==k or k==0
choose(n,k) = {
{ choose(n-1,k)+choose(n-1,k-1) otherwise
The value N should be a static final int. So your program should
look a bit like this:
class PascalTriangle {
static final int N = 5 ;
static int choose( int n, int k ) {
// you write this code
}
public static void main(String [] args) {
// you write this code
}
}
Hints:
- First put your full attention to writing the recursive method
choose. Test it on various n and k until you are sure it is working.
- Next in the main routine write a while loop that prints a single
line of the triangle, for line n, n is a variable.
- Finally, add a loop around the single-line-writer to write the
triangle, but driving n in the correct increments