0

I want a matrix returned as a result. I'm working on netbeans there may be problems with the same but I wanted to ask anyway and I am actually learning object oriented programming basics I have little knowledge of Stackoverflow and could you please explain in some detail? thank you

package matrixmultiply;


public class matrix extends calculate{
    
    private float[][] M1;
    private float[][] M2;
    

 

    @Override
    public float[][] multiply() {
        
    
    float[][] result = new float[getM1().length][getM2().length];

    for(int i = 0; i < getM1().length; i++)
    {
        for(int j = 0; j < getM2().length; j++)
        {
            for(int k = 0; k < getM2().length; k++)
            {
            
                result[i][j] += getM1()[i][k] * getM2()[k][j];
                
            }
        }
    }
    
        System.out.println(result);
    return result;
        
    }

    
    public float[][] getM1() {
        return M1;
    }

   
    public void setM1(float[][] M1) {
        this.M1 = M1;
    }

    
    public float[][] getM2() {
        return M2;
    }

 
    public void setM2(float[][] M2) {
        this.M2 = M2;
    }

  


    

    
}

main class


package matrixmultiply;


public class MatrixMultiply {

  
    public static void main(String[] args) {
        
        matrix matrix1=new matrix();
        
        float[][] m1=new float[3][2];
        float[][] m2=new float[2][3];
        
        matrix1.setM1(m1);
        matrix1.setM2(m2);
        
        matrix1.multiply();
        
    }
    
}




calculate class

package matrixmultiply;


public abstract class calculate {
    
    
    public abstract float[][] multiply();

     
}
``
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • See https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array ... `out.println()` simply doesnt work on arrays the way you would expect it to work. – GhostCat Mar 09 '22 at 15:38
  • But please understand: it is your responsibility to first learn how this place works. Please see [help] to learn how/what to ask here. When you ask about problems with your code, then clearly articulate that. "please review my code" isn't regarded a valid question here. – GhostCat Mar 09 '22 at 15:39
  • Having said that: look into java naming conventions. You get a lot of things wrong. Class names should start UpperCase, and field and variable names all start lowercase. And names like M1 and M2 dont mean much to human readers. Use names that MEAN something. – GhostCat Mar 09 '22 at 15:40
  • And from a conceptual point: so far, there is no sense in having an abstract base class. That only adds complexity at this point ... for no reason whatsoever. So: always try to write the SMALLEST solution possible. – GhostCat Mar 09 '22 at 15:42

0 Answers0