it works!
This commit is contained in:
		@@ -16,16 +16,15 @@ import java.util.ArrayList;
 | 
			
		||||
 */
 | 
			
		||||
@Named(value = "matrixBeanA")
 | 
			
		||||
@ViewScoped
 | 
			
		||||
public class MatrixBeanA implements Serializable
 | 
			
		||||
{
 | 
			
		||||
public class MatrixBeanA implements Serializable {
 | 
			
		||||
 | 
			
		||||
    private int columns = 2;
 | 
			
		||||
    private int rows = 2;
 | 
			
		||||
    ArrayList<ArrayList<String>> matrix = new ArrayList<ArrayList<String>>();
 | 
			
		||||
 | 
			
		||||
    public MatrixBeanA()
 | 
			
		||||
    {
 | 
			
		||||
    public MatrixBeanA() {
 | 
			
		||||
        matrix = new ArrayList<ArrayList<String>>();
 | 
			
		||||
       BigInteger counter = BigInteger.ZERO;
 | 
			
		||||
        BigInteger counter = BigInteger.ZERO;
 | 
			
		||||
        for (int i = 0; i < columns; ++i) {
 | 
			
		||||
            ArrayList<String> row = new ArrayList<>();
 | 
			
		||||
            for (int j = 0; j < rows; ++j) {
 | 
			
		||||
@@ -37,59 +36,58 @@ public class MatrixBeanA implements Serializable
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getColumns()
 | 
			
		||||
    {
 | 
			
		||||
    public int getColumns() {
 | 
			
		||||
        return columns;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setColumns(int columns)
 | 
			
		||||
    {
 | 
			
		||||
    public void setColumns(int columns) {
 | 
			
		||||
        this.columns = columns;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getRows()
 | 
			
		||||
    {
 | 
			
		||||
    public int getRows() {
 | 
			
		||||
        return rows;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setRows(int rows)
 | 
			
		||||
    {
 | 
			
		||||
    public void setRows(int rows) {
 | 
			
		||||
        this.rows = rows;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void listenForKeyUp(int i, int j, String input)
 | 
			
		||||
    {
 | 
			
		||||
    public void listenForKeyUp(int i, int j, String input) {
 | 
			
		||||
        System.out.println("i=" + i);
 | 
			
		||||
        System.out.println("j=" + j);
 | 
			
		||||
        System.out.println("input=" + input);
 | 
			
		||||
        matrix.get(i).set(j, input);
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ArrayList<ArrayList<String>> getMatrix()
 | 
			
		||||
    {
 | 
			
		||||
    public ArrayList<ArrayList<String>> getMatrix() {
 | 
			
		||||
        return matrix;
 | 
			
		||||
    }
 | 
			
		||||
        public void changeRowsColumns() {
 | 
			
		||||
            System.out.println("Columns:" + columns);
 | 
			
		||||
            System.out.println("Rows:" + rows);
 | 
			
		||||
            ArrayList<ArrayList<String>> resizedMatrix = new ArrayList<>();
 | 
			
		||||
            for (int i = 0; i < rows; ++i) {
 | 
			
		||||
 | 
			
		||||
    public void changeRowsColumns() {
 | 
			
		||||
        System.out.println("Columns:" + columns);
 | 
			
		||||
        System.out.println("Rows:" + rows);
 | 
			
		||||
        ArrayList<ArrayList<String>> resizedMatrix = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        // Create a new resized matrix with the updated number of rows and columns
 | 
			
		||||
        for (int i = 0; i < rows; ++i) {
 | 
			
		||||
            ArrayList<String> row = new ArrayList<>();
 | 
			
		||||
            for (int j = 0; j < columns; ++j) {
 | 
			
		||||
                row.add("1");
 | 
			
		||||
            }
 | 
			
		||||
            resizedMatrix.add(row);
 | 
			
		||||
        }
 | 
			
		||||
            // add the data from the old matrix to the new one
 | 
			
		||||
            for (int i = 0; i < columns; ++i) {
 | 
			
		||||
                for (int j = 0; j < rows; ++j) {
 | 
			
		||||
                    if (i < matrix.size() && j < matrix.get(i).size()) {
 | 
			
		||||
                        resizedMatrix.get(i).set(j, matrix.get(i).get(j));
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
        // Copy data from the old matrix to the new resized matrix
 | 
			
		||||
        for (int i = 0; i < rows; ++i) {
 | 
			
		||||
            for (int j = 0; j < columns; ++j) {
 | 
			
		||||
                if (i < matrix.size() && j < matrix.get(i).size()) {
 | 
			
		||||
                    resizedMatrix.get(i).set(j, matrix.get(i).get(j));
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            // overwrite the old matrix
 | 
			
		||||
            matrix = resizedMatrix;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Overwrite the old matrix with the resized matrix
 | 
			
		||||
        matrix = resizedMatrix;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -13,12 +13,11 @@ import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 *
 | 
			
		||||
 *  @author A. V. Markou
 | 
			
		||||
 * @author A. V. Markou
 | 
			
		||||
 */
 | 
			
		||||
@Named(value = "matrixOperations")
 | 
			
		||||
@ViewScoped
 | 
			
		||||
public class MatrixOperations implements Serializable
 | 
			
		||||
{
 | 
			
		||||
public class MatrixOperations implements Serializable {
 | 
			
		||||
 | 
			
		||||
    @Inject
 | 
			
		||||
    MatrixBeanA matrixA;
 | 
			
		||||
@@ -27,81 +26,91 @@ public class MatrixOperations implements Serializable
 | 
			
		||||
 | 
			
		||||
    @Inject
 | 
			
		||||
    MatrixBeanC matrixC;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    edu.slcc.asdv.bl.Matrices matrixManipulator = new edu.slcc.asdv.bl.Matrices();
 | 
			
		||||
 | 
			
		||||
    public String add() {
 | 
			
		||||
        
 | 
			
		||||
        ArrayList<ArrayList<String>>  a =  matrixA.getMatrix();
 | 
			
		||||
        ArrayList<ArrayList<String>>  b =  matrixB.getMatrix();
 | 
			
		||||
        ArrayList<ArrayList<String>>  c =  new ArrayList<ArrayList<String>>();
 | 
			
		||||
        
 | 
			
		||||
        for ( int i=0; i < a.size(); ++i)
 | 
			
		||||
        {
 | 
			
		||||
 | 
			
		||||
        ArrayList<ArrayList<String>> a = matrixA.getMatrix();
 | 
			
		||||
        ArrayList<ArrayList<String>> b = matrixB.getMatrix();
 | 
			
		||||
        ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < a.size(); ++i) {
 | 
			
		||||
            ArrayList<String> row = new ArrayList<>();
 | 
			
		||||
            for ( int j=0; j < a.size(); ++j)
 | 
			
		||||
            {
 | 
			
		||||
            Integer result =   Integer.parseInt ( a.get(i).get(j))
 | 
			
		||||
                                    +
 | 
			
		||||
                        Integer.parseInt ( b.get(i).get(j));
 | 
			
		||||
             
 | 
			
		||||
              row.add( result.toString() );
 | 
			
		||||
             }
 | 
			
		||||
          c.add(row);
 | 
			
		||||
           System.out.println(row);
 | 
			
		||||
            for (int j = 0; j < a.size(); ++j) {
 | 
			
		||||
                Integer result = Integer.parseInt(a.get(i).get(j))
 | 
			
		||||
                        + Integer.parseInt(b.get(i).get(j));
 | 
			
		||||
 | 
			
		||||
                row.add(result.toString());
 | 
			
		||||
            }
 | 
			
		||||
            c.add(row);
 | 
			
		||||
            System.out.println(row);
 | 
			
		||||
        }
 | 
			
		||||
       matrixC.setMatrix( c );
 | 
			
		||||
                return "";
 | 
			
		||||
        matrixC.setMatrix(c);
 | 
			
		||||
        return "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String multiply() {
 | 
			
		||||
        ArrayList<ArrayList<BigInteger>>  a =  convertToBigInteger(matrixA.getMatrix());
 | 
			
		||||
        ArrayList<ArrayList<BigInteger>>  b =  convertToBigInteger(matrixA.getMatrix());
 | 
			
		||||
        ArrayList<ArrayList<String>>  c =  new ArrayList<ArrayList<String>>();
 | 
			
		||||
        c = convertToString(matrixManipulator.multiplyParallel(a, b));
 | 
			
		||||
        printArray(c);
 | 
			
		||||
       matrixC.setMatrix( c );
 | 
			
		||||
        return "";}
 | 
			
		||||
        
 | 
			
		||||
        public String subtract() {
 | 
			
		||||
        
 | 
			
		||||
        ArrayList<ArrayList<String>>  a =  matrixA.getMatrix();
 | 
			
		||||
        ArrayList<ArrayList<String>>  b =  matrixB.getMatrix();
 | 
			
		||||
        ArrayList<ArrayList<String>>  c =  new ArrayList<ArrayList<String>>();
 | 
			
		||||
        
 | 
			
		||||
        for ( int i=0; i < a.size(); ++i)
 | 
			
		||||
        {
 | 
			
		||||
            ArrayList<String> row = new ArrayList<>();
 | 
			
		||||
            for ( int j=0; j < a.size(); ++j)
 | 
			
		||||
            {
 | 
			
		||||
            Integer result =   Integer.parseInt ( a.get(i).get(j))
 | 
			
		||||
                                    -
 | 
			
		||||
                        Integer.parseInt ( b.get(i).get(j));
 | 
			
		||||
             
 | 
			
		||||
              row.add( result.toString() );
 | 
			
		||||
             }
 | 
			
		||||
          c.add(row);
 | 
			
		||||
           System.out.println(row);
 | 
			
		||||
        ArrayList<ArrayList<BigInteger>> a = convertToBigInteger(matrixA.getMatrix());
 | 
			
		||||
        ArrayList<ArrayList<BigInteger>> b = convertToBigInteger(matrixA.getMatrix());
 | 
			
		||||
        ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < a.size(); i++) {
 | 
			
		||||
            ArrayList<String> rowResult = new ArrayList<>();
 | 
			
		||||
            for (int j = 0; j < b.get(i).size(); j++) {
 | 
			
		||||
                BigInteger sum = BigInteger.ZERO;
 | 
			
		||||
                for (int k = 0; k < a.get(0).size(); k++) {
 | 
			
		||||
                    sum = sum.add(a.get(i).get(k).multiply(b.get(k).get(j)));
 | 
			
		||||
                }
 | 
			
		||||
                rowResult.add(sum.toString());
 | 
			
		||||
            }
 | 
			
		||||
            c.add(rowResult);
 | 
			
		||||
        }
 | 
			
		||||
       matrixC.setMatrix( c );
 | 
			
		||||
                return "";
 | 
			
		||||
 | 
			
		||||
        printArray(c);
 | 
			
		||||
        matrixC.setMatrix(c);
 | 
			
		||||
        return "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
    public MatrixBeanC getMatrixC(){return this.matrixC;}
 | 
			
		||||
    public String subtract() {
 | 
			
		||||
 | 
			
		||||
    public MatrixBeanA getMatrixA(){return matrixA;}
 | 
			
		||||
        ArrayList<ArrayList<String>> a = matrixA.getMatrix();
 | 
			
		||||
        ArrayList<ArrayList<String>> b = matrixB.getMatrix();
 | 
			
		||||
        ArrayList<ArrayList<String>> c = new ArrayList<ArrayList<String>>();
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < a.size(); ++i) {
 | 
			
		||||
            ArrayList<String> row = new ArrayList<>();
 | 
			
		||||
            for (int j = 0; j < a.size(); ++j) {
 | 
			
		||||
                Integer result = Integer.parseInt(a.get(i).get(j))
 | 
			
		||||
                        - Integer.parseInt(b.get(i).get(j));
 | 
			
		||||
 | 
			
		||||
                row.add(result.toString());
 | 
			
		||||
            }
 | 
			
		||||
            c.add(row);
 | 
			
		||||
            System.out.println(row);
 | 
			
		||||
        }
 | 
			
		||||
        matrixC.setMatrix(c);
 | 
			
		||||
        return "";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public MatrixBeanC getMatrixC() {
 | 
			
		||||
        return this.matrixC;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public MatrixBeanA getMatrixA() {
 | 
			
		||||
        return matrixA;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public MatrixBeanB getMatrixB() {
 | 
			
		||||
        return matrixB;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public MatrixBeanB getMatrixB(){return matrixB;}
 | 
			
		||||
    
 | 
			
		||||
    /*
 | 
			
		||||
    
 | 
			
		||||
    public ArrayList<ArrayList<BigInteger>> convertToBigInteger(ArrayList<ArrayList<String>> matrix) {
 | 
			
		||||
        ArrayList<ArrayList<BigInteger>> bigInt2DArray = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i < matrix.size(); i++) {
 | 
			
		||||
            ArrayList<BigInteger> columnArray = new ArrayList<BigInteger>();
 | 
			
		||||
            for (int j = 0; j <  matrix.get(i).size(); j++) {
 | 
			
		||||
            for (int j = 0; j < matrix.get(i).size(); j++) {
 | 
			
		||||
                String cellValue = matrix.get(i).get(j);
 | 
			
		||||
                columnArray.add(new BigInteger(cellValue));
 | 
			
		||||
            }
 | 
			
		||||
@@ -110,7 +119,7 @@ public class MatrixOperations implements Serializable
 | 
			
		||||
 | 
			
		||||
        return bigInt2DArray;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public ArrayList<ArrayList<String>> convertToString(ArrayList<ArrayList<BigInteger>> matrix) {
 | 
			
		||||
        int numRows = matrix.size();
 | 
			
		||||
        int numCols = matrix.get(0).size();
 | 
			
		||||
@@ -125,7 +134,7 @@ public class MatrixOperations implements Serializable
 | 
			
		||||
        }
 | 
			
		||||
        return string2DArray;
 | 
			
		||||
    }
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @param matrix
 | 
			
		||||
@@ -143,7 +152,7 @@ public class MatrixOperations implements Serializable
 | 
			
		||||
        System.out.println(output);
 | 
			
		||||
        return output;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    public String printSingleArray(ArrayList<String> matrix) {
 | 
			
		||||
        String output = "\t{";
 | 
			
		||||
        for (int i = 0; i < matrix.size(); ++i) {
 | 
			
		||||
@@ -155,6 +164,5 @@ public class MatrixOperations implements Serializable
 | 
			
		||||
        output += "}";
 | 
			
		||||
        return output;
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -75,7 +75,24 @@ public class MenuBar implements Serializable
 | 
			
		||||
        PrimeFaces.current().ajax().update(idsC);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
   
 | 
			
		||||
   public boolean areMatricesTheSameSize() {
 | 
			
		||||
       int matrixAColumns = matrixOperations.getMatrixA().getColumns();
 | 
			
		||||
       int matrixBColumns = matrixOperations.getMatrixB().getColumns();
 | 
			
		||||
       int matrixCColumns = matrixOperations.getMatrixC().getColumns();
 | 
			
		||||
       
 | 
			
		||||
       int matrixARows = matrixOperations.getMatrixA().getRows();
 | 
			
		||||
       int matrixBRows = matrixOperations.getMatrixB().getRows();
 | 
			
		||||
       int matrixCRows = matrixOperations.getMatrixC().getRows();
 | 
			
		||||
       
 | 
			
		||||
       boolean matrixAEqual = (matrixAColumns == matrixARows);
 | 
			
		||||
       boolean matrixBEqual = (matrixBColumns == matrixBRows);
 | 
			
		||||
       boolean matrixCEqual = (matrixCColumns == matrixCRows);
 | 
			
		||||
       
 | 
			
		||||
       if (matrixAEqual && matrixBEqual && matrixCEqual) {
 | 
			
		||||
           return true;
 | 
			
		||||
       }
 | 
			
		||||
       return false;
 | 
			
		||||
   }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@
 | 
			
		||||
                    <p:submenu id ="submenu_matrices" 
 | 
			
		||||
                               label="Matrix" icon="pi pi-table">
 | 
			
		||||
                        <p:menuitem title="This does addition" 
 | 
			
		||||
                                    id ="menuitem_add" value="Add" disabled="false"
 | 
			
		||||
                                    id ="menuitem_add" value="Add" disabled="#{!menuBar.areMatricesTheSameSize()}"
 | 
			
		||||
                                    action="#{menuBar.add()}"                            
 | 
			
		||||
                                    icon="pi pi-plus" update="id_message "/>
 | 
			
		||||
                        
 | 
			
		||||
@@ -28,7 +28,7 @@
 | 
			
		||||
                                    update="id_message"/>
 | 
			
		||||
                                                  
 | 
			
		||||
                        <p:menuitem title="Subtraction" id ="menuitem_subtract" value="Subtract"
 | 
			
		||||
                                    action="#{menuBar.subtract}"  disabled="false"
 | 
			
		||||
                                    action="#{menuBar.subtract}"  disabled="#{!menuBar.areMatricesTheSameSize()}"
 | 
			
		||||
                                    icon="pi pi-minus" update="id_message"/>
 | 
			
		||||
                    </p:submenu>
 | 
			
		||||
                </p:menubar>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user