Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package module5;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.Arrays;
  5.  
  6. public class Matrix {
  7.    
  8.     // Instance variable to store matrix contents
  9.     private double[][] contents;
  10.    
  11.     // Constructor
  12.     public Matrix(double[][] m){
  13.         this.contents = m; // Set the contents variable to constructor's argument
  14.     }
  15.    
  16.     // Number of rows - Non static
  17.     public int nRows(){
  18.         return this.contents.length;
  19.     }
  20.    
  21.     // Number of columns (in first row) - Non static
  22.     public int nCols(){
  23.         return this.contents[0].length;
  24.     }
  25.    
  26.     // Is current matrix square? - Non static
  27.     public boolean isSquare(){
  28.         if(nRows() == nCols()){
  29.             return true;
  30.         } else {
  31.             return false;
  32.         }
  33.     }
  34.    
  35.     // Create a new matrix containing diagonal elements of original - Non static
  36.     public Matrix diagonal() throws Exception{
  37.         // Determine if matrix is square or not
  38.         if (!this.isSquare()){
  39.             throw new Exception("Matrix is not square");
  40.         } else {
  41.             // Create a new array to store the diagonal values
  42.             double[][] a1 = new double[nCols()][nRows()];
  43.            
  44.             for(int i = 0; i < nCols(); i++){
  45.                 // Step through the diagonal elements only
  46.                 a1[i][i] = this.contents[i][i];
  47.             }
  48.            
  49.             System.out.println(Arrays.deepToString(a1)); // Seems to work
  50.             return new Matrix(a1); // How would i print this? System.out.println(Matrix(a1))???
  51.         }
  52.     }
  53.    
  54.     // Add two matrices - Non static
  55.     public Matrix add(Matrix m2) throws Exception {
  56.         if(this.nCols() != m2.nCols()){
  57.             throw new Exception("Number of coloumns do not match");
  58.         } else if(this.nRows() != m2.nRows()) {
  59.             throw new Exception("Number of rows do not match");
  60.         } else {
  61.             // Create a new array to store the addition
  62.             double[][] m3 = new double[nCols()][nRows()];
  63.            
  64.             // Cycle over each row
  65.             for(int x = 0; x < nRows(); x++){
  66.                 // Within each row, cycle over each column
  67.                 for(int y = 0; y < nCols(); y++){
  68.                     m3[x][y] = this.contents[x][y] + m2[x][y]; // m2 - The type of the expression must be an array type but it resolved to Matrix
  69.                 }
  70.             }
  71.            
  72.             System.out.println(Arrays.deepToString(m3));
  73.             return new Matrix(m3);
  74.         }
  75.     }
  76.        
  77.     public static void main(String[] args) throws Exception {
  78.  
  79.         // Define the contents of an array
  80.         double[][] contents = {
  81.             {1.0,2.1,3.2,4.3},
  82.             {11.5,12.6,13.7,14.8},
  83.             {11.9,12.0,13.1,14.2},
  84.             {11.9,12.0,13.1,14.2}
  85.         };
  86.        
  87.         // Create a new Matrix object with contents
  88.         Matrix m1 = new Matrix(contents);
  89.        
  90.         //System.out.println(m1.nRows());
  91.         //System.out.println(m1.nCols());
  92.         //System.out.println(m1.isSquare());
  93.         System.out.println(m1.diagonal()); 
  94.        
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement