mattryan

Pascal's Triangle

Feb 16th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.09 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class PascalTriangle {
  6.   private static final String NEW_LINE = System.getProperty("line.separator");
  7.  
  8.   private long[][] triangle;
  9.  
  10.   public PascalTriangle(int rowCount) {
  11.     if (rowCount <= 0) {
  12.       return;
  13.     }
  14.     triangle = new long[rowCount][];
  15.     for (int r = 0; r < rowCount; r++) {
  16.       int colCount = r + 1;
  17.       triangle[r] = new long[colCount];
  18.       triangle[r][0] = 1;
  19.       triangle[r][colCount - 1] = 1;
  20.       for (int c = 1; c < colCount - 1; c++) {
  21.         triangle[r][c] = triangle[r - 1][c - 1] + triangle[r - 1][c];
  22.       }
  23.     }
  24.   }
  25.  
  26.   public long getValue(int row, int col) throws IllegalArgumentException {
  27.     if (triangle == null) {
  28.       return 0;
  29.     }
  30.     if (col > row) {
  31.       throw new IllegalArgumentException(
  32.           "Invalid column entry, column value cannot be greater than row value");
  33.     }
  34.     return triangle[row - 1][col - 1];
  35.   }
  36.  
  37.   public String getTriangleDisplay() {
  38.     if (triangle == null) {
  39.       return null;
  40.     }
  41.     StringBuilder triangleBuilder = new StringBuilder();
  42.     for (int r = 0; r < triangle.length; r++) {
  43.       triangleBuilder.append(r != 0 ? NEW_LINE : "");
  44.       for (int c = 0; c < triangle[r].length; c++) {
  45.         triangleBuilder.append(c != 0 ? '\t' : "");
  46.         triangleBuilder.append(triangle[r][c]);
  47.       }
  48.     }
  49.     return triangleBuilder.toString();
  50.   }
  51.  
  52.   public static void main(String[] args) throws Exception {
  53.     BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  54.     int row = 0;
  55.     int col = 0;
  56.  
  57.     String line;
  58.     while ((line = prompt(in)) != null) {
  59.       if (line.startsWith("exit")) {
  60.         break;
  61.       }
  62.       String[] input = line.split(",\\s+");
  63.       try {
  64.         row = Integer.parseInt(input[0]);
  65.         if (row <= 0) {
  66.           throw new IllegalArgumentException();
  67.         }
  68.       } catch (IllegalArgumentException e) {
  69.         System.err
  70.             .println("Invalid row entry, expected positive integer but got "
  71.                 + input[0]);
  72.         continue;
  73.       }
  74.       try {
  75.         col = Integer.parseInt(input[1]);
  76.         if (col <= 0) {
  77.           throw new IllegalArgumentException();
  78.         }
  79.       } catch (IllegalArgumentException e) {
  80.         System.err
  81.             .println("Invalid column entry, expected positive integer but got "
  82.                 + input[1]);
  83.         continue;
  84.       }
  85.  
  86.       PascalTriangle triangle = new PascalTriangle(row);
  87.       long value = 0;
  88.       try {
  89.         value = triangle.getValue(row, col);
  90.       } catch (IllegalArgumentException e) {
  91.         System.err.println(e.getMessage());
  92.         continue;
  93.       }
  94.  
  95.       System.out.println("Output: " + value);
  96.       System.out.println();
  97.       System.out.println(triangle.getTriangleDisplay());
  98.       System.out.println();
  99.     }
  100.   }
  101.  
  102.   private static String prompt(BufferedReader in) throws IOException {
  103.     System.out.println();
  104.     System.out.print("Enter Input (type 'exit' to end): ");
  105.     return in.readLine();
  106.   }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment