Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
package homework2;

public class Homework2 {
    public static void main(String[] args){
    int num1 = (int) (Math.random()*(10-3+1)+3);
    int num2 = (int) (Math.random()*(10-3+1)+3);

    double[][] doubMatrix1 = new double[num1][num2];
    double[][] doubMatrix2 = new double[num1][num2];
    double[][] doubMatrix3 = new double[num1][num2];

    doubMatrix1 = getdoubMatrix(num1,num2);
    doubMatrix2 = getdoubMatrix(num1,num2);
    doubMatrix3 = addMatrices(doubMatrix1, doubMatrix2, num1, num2);        
    printDoubMatrix("First matrix", doubMatrix1);
    printDoubMatrix("Second matrix", doubMatrix2);
    printDoubMatrix("Result of adding", doubMatrix3);
    doubMatrix2 =transposeMatrix(num1,num2, doubMatrix2);
    printDoubMatrix("Second matrix transposed", doubMatrix2);
    doubMatrix3 = multipyMatrices(doubMatrix1, doubMatrix2);
    printDoubMatrix("Result of multiplying", doubMatrix3);




}

public static double[][] getdoubMatrix(int num1,int num2){

    double[][] tempArray = new double[num1][num2];
    for(int i = 0;i < tempArray.length;i++)
        for(int j = 0;j < tempArray[i].length;j++)
        {
            tempArray[i][j] = Math.random() * (100);                
        }
    return tempArray;
}

public static double[][] addMatrices(double[][] doubMatrix1, double[][] doubMatrix2,int d1,int d2)
{

    double[][] tempArray = null;
    if(doubMatrix1.length == doubMatrix2.length)
        if(doubMatrix1[0].length == doubMatrix2[0].length)
        {
             tempArray = new double[d1][d2];
            for(int i = 0; i< doubMatrix1.length;i++)
                  for(int j = 0; j< doubMatrix1[i].length;j++ )
                  {
                      tempArray[i][j] = doubMatrix1[i][j] + doubMatrix2[i][j];
                  }
        }
        else
        {
            return tempArray = new double[0][0];

        }

    return tempArray;
}

public static void printDoubMatrix(String text,double[][] doubMatrix1){

    System.out.println(text);
    for(int i = 0; i< doubMatrix1.length;i++)
    {
          for(int j = 0; j< doubMatrix1[i].length;j++ ) {                    
              System.out.printf("%9.1f", doubMatrix1[i][j]);                
          }
          System.out.println();
    }       
}

public static double[][] transposeMatrix(int num1, int num2, double[][] doubMatrix2){
    double[][] tempArray = new double[num2][num1];
    for(int i = 0;i < num1;i++)
        for(int j = 0;j < num2;j++)
        {
            tempArray[j][i] = doubMatrix2[i][j];               
        }    
    return tempArray;
}

public static double[][] multipyMatrices(double[][] doubMat1, double[][] doubMat2)

{                   
        double[][] tempArray = new double[doubMat1.length][doubMat2[0].length];
        if(doubMat1[0].length == doubMat2.length)
            if(doubMat1.length == doubMat2[0].length)
        {

                 for (int i = 0; i <doubMat1.length; i++)
                   {
                       for(int j = 0; j < doubMat2[0].length; j++)
                        {

                           for (int k = 0; k < doubMat2.length; k++)
                           {
                               tempArray[i][j] = tempArray[i][j] + doubMat1[i][k]*doubMat2[k][j];

                           }
                        }
                   }
        }
            else
            {

                return tempArray = new double[0][0];
            }
        return tempArray;
}

}

Hi, I've just finished this code, can some one review if this code is working correctly, please check if the transposed method and the multiplying method is working correctly and printing the right output.

thank you very much

this is my output

First matrix

 35.4     95.9     66.4     40.4     13.3     13.7
 10.9     18.7     92.3     30.0     57.2     68.5
 46.0     68.6     60.2     46.9     11.3     11.4
 87.7     82.0     15.8     82.6     93.2      8.2
 73.1     15.4     24.1     66.5     32.9     11.7
 19.8      8.4     13.2     63.4     63.5     73.9
 42.5     52.2     73.0      1.1     10.2     78.2
 56.2     40.6     55.9      3.6      9.3     12.9
 28.3      9.2      8.7     46.5     11.5     77.4

Second matrix

 30.1     78.6     44.7     12.5     31.8     83.5
 59.5      1.2     54.4     16.2     22.0     98.1
 24.0     25.7     92.2     92.9     90.9     95.9
  8.7     79.7      3.1     18.0     66.1      1.9
 89.7     85.1     74.7     89.5     27.8     27.4
 85.1     43.4     74.1     72.2     15.9     43.0
 71.2     23.1     41.3     72.5     78.8      1.1
 87.1     66.5     37.6     78.6     33.9     73.3
 48.4     70.5     43.6     22.5     35.7     13.2

Result of adding

 65.5    174.5    111.0     52.9     45.1     97.2
 70.4     19.9    146.7     46.2     79.2    166.6
 70.0     94.3    152.4    139.7    102.2    107.3
 96.4    161.8     18.9    100.6    159.4     10.0
162.8    100.5     98.8    156.0     60.7     39.0
104.9     51.8     87.2    135.6     79.4    116.9
113.7     75.3    114.3     73.6     89.0     79.3
143.3    107.1     93.5     82.3     43.1     86.2
 76.7     79.7     52.3     68.9     47.3     90.6

Second matrix transposed

 30.1     59.5     24.0      8.7     89.7     85.1     71.2     87.1     48.4
 78.6      1.2     25.7     79.7     85.1     43.4     23.1     66.5     70.5
 44.7     54.4     92.2      3.1     74.7     74.1     41.3     37.6     43.6
 12.5     16.2     92.9     18.0     89.5     72.2     72.5     78.6     22.5
 31.8     22.0     90.9     66.1     27.8     15.9     78.8     33.9     35.7
 83.5     98.1     95.9      1.9     27.4     43.0      1.1     73.3     13.2

Result of multiplying

 13644.0   8124.7  15701.1   9788.3  20645.5  15804.4  11466.2  16578.5  12927.1
 13839.9  14159.7  23804.4   6319.7  15612.6  14600.2  11773.1  14979.8   9492.2
 11370.3   8221.8  14886.3   7667.3  19284.5  15409.7  11648.3  15735.2  11295.1
  14484.4  10364.5  22593.6  15018.4  26241.1  20000.4  22139.9  23941.4  16011.0
   7349.0   8620.8  14654.6   5336.4  16859.7  14506.6  13984.9  15499.1   8499.9
  10832.3  11576.3  20651.2   6358.6  12938.4  11791.9  11826.7  15331.7   6794.0
  15516.4  14480.1  17621.0   5597.5  16226.0  14893.5   8214.2  16073.9  10339.5
   8798.8   7960.4   9956.4   4600.3  13605.0  11650.7   8254.8  11235.6   8599.1
  9379.3  10766.4  14501.3   2751.1  10572.3  10319.6   6950.2  13122.3   4877.7
share|improve this question

closed as off topic by palacsint, Jeff Vanzella, Yannis, Corbin, James Khoury Oct 18 '12 at 0:53

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

up vote 3 down vote accepted

First of all I would suggest to create class Matrix with the following structure:

class Matrix{

private double[][] data;

/* Creates a matrix with given two-dimension array */
public Matirx(double[][] data) { ... }

/* Creates a matrix with random numbers from given range */
public Matrix(double min, double max) { ... }

public Matrix transpose () { ... }

public Matrix add(Matrix second) { ... }

public Matrix multiply(Matrix second) { ... }

protected double[][] getData() { ... }

@Override
public String toString() { ... }

}

You can also add the private int fields for marix size and use it into transpose method instead of passing arguments.

Please, note that your Matrix class is immutable. Your methods should return a new matrices without changing the original ones.

Instead of printDoubMatrix() use toString() method.

After that in your main class just create two instances of Matrix classes and make use of them.

share|improve this answer
Do you think matrix should be immutable or mutable? I could see arguments either way – jozefg Oct 17 '12 at 12:08
2  
I would better make it immutable to keep the original matrix. Of course in this context both solutions will be ok, I just suggested the one I like more =) – Alex Stybaev Oct 17 '12 at 13:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.