All pastes #350398 Raw Edit

Lerneaen_Hydra

public java v1 · immutable
·published 2007-02-11 12:21 UTC
rendered paste body
 import java.util.*;  /** Lab 3, Assignment 2, test-like main class that asks for a size and prints the matrix*/  public class ass2{     public static void main(String [] args){         int degree;         Scanner s = new Scanner(System.in);         System.out.println("How large?");         degree = s.nextInt();         Matrix m = new Matrix(degree);         m.createArray();         m.printMatrix();     } }  /** Creates/outputs a pretty matrix for Lab3, Assignment 2 */ class Matrix{     private int degree;          /**     Creates a new matrix for Lab 3, Assignment 2     @param degree the degree of the matrix (N X N)     */     public Matrix(int degree){     if(degree<=0){         throw new IllegalArgumentException("Degree must be a positive integer");     }     else{         this.degree=degree;     }     }          /**     Populates the matrix with the correct values     @return int returns the matrix as a two-dimensional int     */     public int[][] createArray(){         int [][] matrix = new int[degree][degree];         //first init row 0 to 0         for(int i=0;i<=degree;i++){             matrix [0][i]=0;         }         //then set col 0 to 1         for(int j=0;j<=degree;j++){             matrix[j][0]=1;         }         //then do the rest of the calcs and fill in the matrix         for(int j=1;j<=degree;j++){        //do row             for(int i=1;i<=degree;i++){    //do element                 matrix[i][j]=matrix[i-1][j-1]+matrix[i-1][j];             }         }         return matrix[][];     }          /**     Redfines the degree of the matrix     @param int the new degree of the matrix (N X N)     */     public void setDegree(int degree){     if(degree<=0){         throw new IllegalArgumentException("Degree must be a positive integer");     }     else{         this.degree=degree;     }     }          /**     Prints the matrix to the terminal     */     public void printMatrix(){         for(int j=1;j<=degree;j++){        //do row             for(int i=1;i<=degree;i++){    //do element                 if(matrix[i][j]==0){                     System.out.print("  ");                 }                 else{                     System.out.print(matrix[i][j]+" ");                 }             }         }     } }