Class Matrix


  • public class Matrix
    extends java.lang.Object
    Objects of this class can store integer values in form of a two-dimensional matrix.
    Since:
    18.04.2015 15:37:20
    Author:
    Nico Danneberg
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int[] data
      Array to store the data.
      private int height
      The height of this matrix.
      private int width
      The width of this matrix.
    • Constructor Summary

      Constructors 
      Constructor Description
      Matrix​(int w)
      Creates a square matrix with the given width for both dimensions.
      Matrix​(int w, int h)
      Creates a matrix with the given dimensions.
    • Method Summary

      Modifier and Type Method Description
      long findMaxProduct​(boolean diagonals)
      Checks every row / column to find the maximum product of all elements in a row / column.
      int get​(int col, int row)
      Read the value of data at the given coordinates.
      Matrix getSection​(int x, int y, int w)
      Reads a square sub-matrix out of the current object.
      Matrix getSection​(int x, int y, int w, int h)
      Reads a sub-matrix out of the current object.
      boolean isSquare()
      Checks, if this matrix is square.
      static Matrix load​(java.lang.String file, int w)
      Loads the data of a new square matrix from a file.
      static Matrix load​(java.lang.String file, int w, int h)
      Loads the data of a new square matrix from a file.
      boolean set​(int col, int row, int value)
      Sets the value at the given coordinates.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • data

        private int[] data
        Array to store the data. The two dimensions are mapped into one.
      • width

        private int width
        The width of this matrix.
      • height

        private int height
        The height of this matrix.
    • Constructor Detail

      • Matrix

        public Matrix​(int w)
        Creates a square matrix with the given width for both dimensions.
        Parameters:
        w - the width for both dimensions
      • Matrix

        public Matrix​(int w,
                      int h)
        Creates a matrix with the given dimensions.
        Parameters:
        w - the width of the matrix
        h - the height of the matrix
    • Method Detail

      • get

        public int get​(int col,
                       int row)
        Read the value of data at the given coordinates.
        Parameters:
        col - the column
        row - the row
        Returns:
        the value at data[ column, row ]
      • getSection

        public Matrix getSection​(int x,
                                 int y,
                                 int w)
        Reads a square sub-matrix out of the current object.
        Parameters:
        x - starting column
        y - starting row
        w - the width of the sub-matrix
        Returns:
        the sub-matrix
      • getSection

        public Matrix getSection​(int x,
                                 int y,
                                 int w,
                                 int h)
        Reads a sub-matrix out of the current object.
        Parameters:
        x - starting column
        y - starting row
        w - the width of the sub-matrix
        h - the height of the sub-matrix
        Returns:
        the sub-matrix
      • set

        public boolean set​(int col,
                           int row,
                           int value)
        Sets the value at the given coordinates.
        Parameters:
        col - the column
        row - the row
        value - the new value
        Returns:
        true, if the new value has stores successfully
      • findMaxProduct

        public long findMaxProduct​(boolean diagonals)
        Checks every row / column to find the maximum product of all elements in a row / column. If wanted the diagonals are checked, too.
        Parameters:
        diagonals - if true and the width is equal to height the diagonals are checked, too
        Returns:
        the maximum product in a row, column or diagonal
      • isSquare

        public boolean isSquare()
        Checks, if this matrix is square.
        Returns:
        true, if matrix is square
      • load

        public static Matrix load​(java.lang.String file,
                                  int w)
        Loads the data of a new square matrix from a file. Only the given width (and height) is read. Additional digits are ignored.
        Parameters:
        file - the file to read from
        w - the dimensions of the new matrix
        Returns:
        the new square matrix
      • load

        public static Matrix load​(java.lang.String file,
                                  int w,
                                  int h)
        Loads the data of a new square matrix from a file. Only the given width and height is read. Additional digits are ignored.
        Parameters:
        file - the file to read from
        w - the width of the new matrix
        h - the height of the new matrix
        Returns:
        the new matrix