JAVA Matrix with emphasis on Abstract Data Types and Mutabil
JAVA Matrix with emphasis on Abstract Data Types and Mutability
If you answer this question, please do so in an EASY-TO-READ manner, ie DO NOT WRITE YOUR ANSWER ON PAPER THEN TAKE A PICTURE OF IT! Type it out and copy/paste, or use screenshots from your own java IDE. Keep in mind I\'m on NetBeans 8.0.2. Also be sure to keep the same nomenclature for the classes/methods and follow the documentation that tells you how each method should work. I\'ve seperated the question and the two classes below with lines of \"--------\" to make it easier to read.
If you do not follow these instructions then I will vote you down for wasting my question.
---------------------------------------------------------------
---------------------------------------------------------------
For this assignment, you will implement the provided matrix interface (Matrix.java). This interface defines not only which methods you must implement, but gives documentation that describes how they should work. Already provided for you is a base to modify (base.java). The base contains a currently empty matrix class as well as some simple testing code. In order for it to be a reliable type, we will implement it as an immutable type. Creating this ADT (abstract data type) will involve creating 9 methods:
• MyMatrix(double[][] matrix) - a constructor. [4 points]
• public double getElement(int y, int x) - see interface. [2 points]
• public int getRows() - see interface. [2 points]
• public int getColumns() - see interface. [2 points]
• public MyMatrix scale(double scalar) - see interface. [5 points]
• public MyMatrix plus(Matrix other) - see interface. [5 points]
• public MyMatrix minus(Matrix other) - see interface. [5 points]
• boolean equals(Object other) - see interface. [5 points] Recall that methods have two parts: a signature (i.e. its name, type, and what parameters it takes), a body (the actual code that implements the method\'s functionality).
• String toString() - see interface. [5 points].
Be aware that some of the methods throw exceptions - this should be implemented.
---------------------------------------------------------------
/**
 * An implementation of the Matrix ADT (abstract data type). Provides four basic operations over an
 * immutable type.
 */
 public class base implements Matrix {
   
     //TODO: implement interface.
   
     /**
      * Entry point for matrix testing.
      * @param args the command line arguments
      */
     public static void main(String[] args) {
       
         int[][] data1 = new int[0][0];
         int[][] data2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
         int[][] data3 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
       
         Matrix m1 = new ser222_unit12b_hw02_base(data1);
         Matrix m2 = new ser222_unit12b_hw02_base(data2);
         Matrix m3 = new ser222_unit12b_hw02_base(data3);
       
         System.out.println(\"m1 --> Rows: \" + m1.getRows() + \" Columns: \" + m1.getColumns());
         System.out.println(\"m2 --> Rows: \" + m2.getRows() + \" Columns: \" + m2.getColumns());
         System.out.println(\"m3 --> Rows: \" + m3.getRows() + \" Columns: \" + m3.getColumns());
       
         //check for reference issues
         System.out.println(\"m2 -->\ \" + m2);
         data2[1][1] = 101;
         System.out.println(\"m2 -->\ \" + m2);
        //test equals
         System.out.println(\"m2==null: \" + m2.equals(null));             //false
         System.out.println(\"m3==\\\"MATRIX\\\": \" + m2.equals(\"MATRIX\"));   //false
         System.out.println(\"m2==m1: \" + m2.equals(m1));                 //false
         System.out.println(\"m2==m2: \" + m2.equals(m2));                 //true
         System.out.println(\"m2==m3: \" + m2.equals(m3));                 //false
       
         //test operations (valid)
         System.out.println(\"2 * m2:\ \" + m2.scale(2));
         System.out.println(\"m2 + m3:\ \" + m2.plus(m3));
         System.out.println(\"m2 - m3:\ \" + m2.minus(m3));
       
         //test operations (invalid)
         //System.out.println(\"m1 + m2\" + m1.plus(m2));
         //System.out.println(\"m1 - m2\" + m1.minus(m2));
     }
 }
---------------------------------------------------------------
/**
 * A simple matrix ADT (abstract data type).
 */
 public interface Matrix {
   
     /**
      * Returns the element at particular point in the matrix.
      * @param y y position
      * @param x x position
      * @return element
      */
     public int getElement(int y, int x);
    /**
      * Returns the number of rows in the matrix.
      * @return rows
      */
     public int getRows();
   
     /**
      * Returns the number of columns in the matrix.
      * @return columns
      */
     public int getColumns();
           
     /**
      * Returns this matrix scaled by a factor. That is, computes kA where k is a
      * constant and A is a matrix (this object).
      *
      * @param scalar scalar
      * @return matrix
      */
     public Matrix scale(int scalar);
   
     /**
      * Returns this matrix added with another matrix. That is, computes A+B
      * where A and B are matrices (this object, and another respectively).
      * @param other addend
      * @return matrix
      * @throws RuntimeException if matrices do not have matching dimensions.
      */
     public Matrix plus(Matrix other);
   
     /**
      * Returns this matrix subtracted by another matrix. That is, computes A-B
      * where A and B are matrices (this object, and another respectively).
      * @param other subtrahend
      * @return matrix
      * @throws RuntimeException if matrices do not have matching dimensions.
      */
     public Matrix minus(Matrix other);
   
     /**
      * Returns true if this matrix matches another matrix.
      * @param other another matrix
      * @return equality
      */
     @Override
     public boolean equals(Object other);
   
     /**
      * Returns a string representation of this matrix. A new line character will
      * separate each row, while a space will separate each column.
      * @return string representation
      */
     @Override
     public String toString();
 }
Solution
//Matrix.java
public interface Matrix
 {
public double getElement(int y, int x);
public int getRows();
public int getColumns();
   public Matrix scale(int scalar);
 
    public Matrix plus(Matrix other);
 
    public Matrix minus(Matrix other);
   @Override
    public boolean equals(Object other);
   @Override
    public String toString();
 }
//Base.java
public class Base implements Matrix
 {
    private double[][] evalMatrix;
    private int rows=0;
    private int columns=0;
    public Base(double[][] matrix)
    {
        if(matrix.length==0)
        {
            rows = 0;
            columns = 0;
        }
        else
        {
            rows = matrix.length;
            columns = matrix[0].length;
         
        }    
        evalMatrix = new double[rows][columns];
        evalMatrix = matrix;
    }
    public Base(int x, int y)
    {
        rows = x;
        columns = y;
        evalMatrix = new double[x][y];
    }
    public double[][] getMatrix()
    {
        return evalMatrix;
    }
 
    @Override
    public double getElement(int y, int x)
    {
        return evalMatrix[y][x];
    }
 
    @Override
    public int getRows()
    {
        return rows;
    }
 
    @Override
    public int getColumns()
    {
        return columns;
    }
 
    @Override
    public Matrix scale(int scalar) {
        double newValue=0;
        int i,j;
        Base matrix1=new Base(getRows(),getColumns());
        for(i=0;i<rows;i++)
        {
            for(j=0;j<columns;j++)
            {
                newValue = (getElement(i,j)*scalar);
                matrix1.setElement(i,j,newValue);
            }        
        }
        return matrix1;
    }
    @Override
    public Matrix plus(Matrix other) {
        double total=0;
        int i,j;
        if((getRows()==other.getRows()) && (getColumns()==other.getColumns()))
        {
            Base matrix2=new Base(other.getRows(),getColumns());
            for(i=0;i<other.getRows();i++)
            {
                for(j=0;j<other.getColumns();j++)
                {
                    total = (getElement(i, j) + other.getElement(i, j)) ;
                    matrix2.setElement(i,j,total);
                }
             
             }
            return matrix2;
        }
        else
            return new Base(0,0);
    }
 
    @Override
    public Matrix minus(Matrix other) {
        double difference=0;
        int i,j;
        if((getRows()==other.getRows()) && (getColumns()==other.getColumns()))
        {
            Base matrix3=new Base(other.getRows(),getColumns());
            for(i=0;i<other.getRows();i++)
            {
                for(j=0;j<other.getColumns();j++)
                {
                    difference = (getElement(i, j) - other.getElement(i, j)) ;
                    matrix3.setElement(i,j,difference);
                }            
             }
            return matrix3;
        }
        else
            return new Base(0,0);
    }
 
    public void setElement(int i, int j, double val)
    {    
        evalMatrix[i][j] = val;
    }
 
    public boolean equals(Object other)
    {
        if(other == this)
        {
            return true;
        }
        else
            return false;
    }
 
    public String toString()
    {
        StringBuffer buffer = new StringBuffer(\"[\");
        for (int i = 0; i<rows; i++){
            buffer.append(\"[\");
            for (int j=0; j<columns; j++)
                buffer.append(\" \"+evalMatrix[i][j]+\" \");
            buffer.append(\"]\");
        }
        return buffer+\"]\";
    }
 
    public static void main(String[] args)
    {
        double[][] data1 = new double[0][0];
        double[][] data2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        double[][] data3 = {{1, 4, 7}, {2, 5, 8}, {3, 6, 9}};
       Base m1 = new Base(data1);
        Base m2 = new Base(data2);
        Base m3 = new Base(data3);
       System.out.println(\"m1 --> Rows: \" + m1.getRows() + \" Columns: \" + m1.getColumns());
        System.out.println(\"m2 --> Rows: \" + m2.getRows() + \" Columns: \" + m2.getColumns());
        System.out.println(\"m3 --> Rows: \" + m3.getRows() + \" Columns: \" + m3.getColumns());
       //check for reference issues
        System.out.println(\"m2 -->\ \" + m2);
        data2[1][1] = 101;
        System.out.println(\"m2 -->\ \" + m2);
       //test equals
        System.out.println(\"m2==null: \" + m2.equals(null)); //false
        System.out.println(\"m3==\\\"MATRIX\\\": \" + m2.equals(\"MATRIX\")); //false
        System.out.println(\"m2==m1: \" + m2.equals(m1)); //false
        System.out.println(\"m2==m2: \" + m2.equals(m2)); //true
        System.out.println(\"m2==m3: \" + m2.equals(m3)); //false
       //test operations (valid)
        System.out.println(\"2 * m2:\ \" + m2.scale(2));
        System.out.println(\"m2 + m3:\ \" + m2.plus(m3));
        System.out.println(\"m2 - m3:\ \" + m2.minus(m3));
    }
 }
 /*
 output:
m1 --> Rows: 0 Columns: 0
 m2 --> Rows: 3 Columns: 3
 m3 --> Rows: 3 Columns: 3
 m2 -->
 [[ 1.0 2.0 3.0 ][ 4.0 5.0 6.0 ][ 7.0 8.0 9.0 ]]
 m2 -->
 [[ 1.0 2.0 3.0 ][ 4.0 101.0 6.0 ][ 7.0 8.0 9.0 ]]
 m2==null: false
 m3==\"MATRIX\": false
 m2==m1: false
 m2==m2: true
 m2==m3: false
 2 * m2:
 [[ 2.0 4.0 6.0 ][ 8.0 202.0 12.0 ][ 14.0 16.0 18.0 ]]
 m2 + m3:
 [[ 2.0 6.0 10.0 ][ 6.0 106.0 14.0 ][ 10.0 14.0 18.0 ]]
 m2 - m3:
 [[ 0.0 -2.0 -4.0 ][ 2.0 96.0 -2.0 ][ 4.0 2.0 0.0 ]]
 */







