A Way For Learning

Program - Matrix

No comments
Write a program, to implement ( abstract data type ) Matrix. Following are the operations:
   Input Values - Given a Matrix represented as a string, parse and fill values in Matrix. (Use the constructor Matrix(String x))
   Add - Add 2 matrices
   Transpose - Transpose of a Matrix
   Multiply - Multiplication of 2 matrices

# Methods:
   1) Matrix(matrix): Constructor with a String as parameter, Convert into Matrix.
   2) add(Matrix): Takes a Matrix as parameter, adds it with current Matrix and returns the output Matrix in String form. Implement toString() to return your output as a string in correct format.
   3) transpose(): Returns the transposed Matrix in String form. Implement toString() to return your output as a string in correct format.
   4) multiply(): Takes a Matrix as parameter, multiply it with current Matrix and returns the output Matrix in String form. Implement toString() to return your output as a string in correct format.
   5) toString(): Converts the given input into String format.

#Input Format:
In a given String Matrix, Rows are space seperated and elements of rows are comma seperated.
Example:
[[0,0,0]
[0,0,0]
[0,0,0]] becomes "0,0,0 0,0,0 0,0,0"


Program:
import java.util.*;
import java.lang.*;
class Matrix {

    public int[][] matrix;
    public int numRows;
    public int numCols;

    public Matrix() {
        numRows=0;
        numCols=0;

    }

    public void set(int[][] c)
    {
        this.matrix=c;

    }

    public String rotateRightBy90Degrees()
    {
        Matrix m3 = new Matrix();
        m3.numRows=numCols;
        m3.numCols=numRows;
        m3.matrix=new int[m3.numRows][m3.numCols];
        for(int i=0;i<numRows;i++)
        {
            for(int j=0;j<numCols;j++)
            {
                m3.matrix[j][i]=this.matrix[i][j];
            }
        }
        int count;
        if (m3.numRows%2==0)
            count=m3.numRows/2;
        else
            count=(m3.numRows/2)+1;
        int start=0,temp;
        while(count>start)
        {
            for(int j=0;j<m3.numCols;j++)
            {
                temp=m3.matrix[start][j];
                m3.matrix[start][j]=m3.matrix[count][j];
                m3.matrix[count][j]=temp;
            }
            count--;
            start++;

        }
        return m3.toString();
    }


    public Matrix(String matrix) {
        //  Your Code Goes Here.
        for(int i=0;i<matrix.length();i++)
        { //  System.out.println("1st for..");
            char c=matrix.charAt(i);

            if(c==',')
                numCols+=1;
            if(c==' ')
                break;
        }
        ++numCols;
        //System.out.println("numCols="+(++numCols));
       
        for(int i=0;i<matrix.length();i++)
            {
                if(matrix.charAt(i)==' ')
                numRows++;
            //    System.out.println("2nd for");
}
        this.matrix=new int[++numRows][numCols];
        //System.out.println("numRows="+numRows+"\n");
        int k=0,i=0,j=0;
        while(k<matrix.length())
        {

            if(j==numCols)
            {    j=0;
                i++;
            }
           // System.out.println(matrix.charAt(k));
            if(matrix.charAt(k)!=' '&&matrix.charAt(k)!=',')
            {this.matrix[i][j]=Character.getNumericValue(matrix.charAt(k));
//            System.out.println("i="+i+" j="+j+" el="+this.matrix[i][j]);
            j++;
            }
            k++;
    }
}

    public String add(Matrix m) {
        // Don't modify the following code
        Matrix m3 = new Matrix();
        m3.numRows=this.numRows;
        m3.numCols=this.numCols;
        m3.matrix=new int[this.numRows][this.numCols];
         if(m.numCols!=numCols||m.numRows!=numRows)
        {
            return "Can't perform addition on the two matrices";
       
        }
        for(int i=0;i<this.numRows;i++)
        {
            for(int j=0;j<this.numCols;j++)
            {
                m3.matrix[i][j]=m.matrix[i][j]+this.matrix[i][j];
            }
        }
        return m3.toString();
    }

    public String transpose() {
    Matrix m3 = new Matrix();
        System.out.println(this);
        m3.numRows=numCols;
        m3.numCols=numRows;
        m3.matrix=new int[m3.numRows][m3.numCols];
        for(int i=0;i<numRows;i++)
        {
            for(int j=0;j<numCols;j++)
            {
                m3.matrix[j][i]=this.matrix[i][j];
            }
        }
        return m3.toString();
    }

    public String multiply(Matrix m) {
    Matrix m3 = new Matrix();
        int sum=0;
        m3.numRows=numRows;
        m3.numCols=m.numCols;
        m3.matrix=new int[m3.numRows][m3.numCols];
        int[][] c=new int[m3.numRows][m3.numCols];
        if(m.numRows!=numCols)
        {
            return "Can't perform multiplication on the two matrices";
        }
        for(int i=0;i<this.numRows;i++)
        {
            for(int j=0;j<m.numCols;j++)
            {
               
                m3.matrix[i][j]=0;
                for(int k=0;k<this.numCols;k++)
                {
                    sum=sum+matrix[i][k]*m.matrix[k][j];
                }
                m3.matrix[i][j]=sum;
                sum=0;
            }
        }
        return m3.toString();
    }

    public String toString() {
    int i, j;
    StringBuffer sb = new StringBuffer();
    for(i=0; i<numRows; i++) {
    for(j=0; j<numCols-1; j++)
    sb.append(matrix[i][j] + ",");
    if(j==numCols)
    sb.append(matrix[i][j] + ",");
    else
    sb.append(matrix[i][j] + " ");
    }
    return (sb.toString()).trim();
    }
}
public class cspp2_w4_q1 {
    public static void main(String[] args) {
        //you can call your function here

        Matrix m1=new Matrix("1,2,3 4,5,6");
        Matrix m2=new Matrix("1,1 2,2");
        System.out.println("m1="+m1);
        System.out.println("m2="+m2);
        System.out.println("Rotation of m1: "+m1.rotateRightBy90Degrees());
        System.out.println("Rotation of m2: "+m2.rotateRightBy90Degrees());

        }
}

No comments :

Post a Comment