Implement a class to recursively calculate the n-th line of


Implement a class to recursively calculate the n-th line of Pascal's Triangle:

 1: 1

 2: 1 1

 3: 1 2 1

 4: 1 3 3 1

 5: 1 4 6 4 1

 6: 1 5 10 10 5  1

 7: 1 6 15 20 15 6  1

 8: 1 7 21 35 35 21 7 1

 9: 1 8 28 56 70 56 28 8 1

10: 1 9 36 84 126 126 84 36 9 1     

Important questions to ask yourself:

  • What is the base case?
  • What is the recursive case?
  • What should the method return?

Your class should be called Pascal and have one static method called triangle that takes an argument 'n' and returns the n-th line of Pascal's Triangle.

Here is the code for the main method that will call the method you write.

public class Main {

   public static void main(String[] args) {

       int n = args.length == 1 ? Integer.parseInt(args[0]) : 1;

       for (int i = 1; i <= n; ++i) {

           int[] arr = Pascal.triangle(i);

           System.out.print((i < 10 ? " " : "") + i + ": ");

           for (int j : arr) {

               System.out.print(j + " ");

           }

           System.out.println();

       }

   }

}

class Pascal {

   public static int[] triangle(int n) {

       return new int[]{0};

   }

}

Solution Preview :

Prepared by a verified Expert
Basic Computer Science: Implement a class to recursively calculate the n-th line of
Reference No:- TGS02711939

Now Priced at $20 (50% Discount)

Recommended (97%)

Rated (4.9/5)