-2

I have trouble with my java homework. I am making a Matrice class and i have trouble with the shallow constructor that take another Matrice as argument.

Public class Matrice implements IMatrice{

private static int numLignes ;
private static int numColonnes ;
private static ArrayList<Double> elements  ;

public Matrice(int numLignes, int numColonnes ) {
        this.numColonnes = numColonnes ;
        this.numLignes = numLignes ;

}

public Matrice(int numLignes, int numColonnes , double valeurs ){
    this(numLignes,numColonnes);
    this.elements = new ArrayList<>(numLignes * numColonnes);
    for(int i = 0 ;i< numLignes * numColonnes;i++){
        elements.add(valeurs);
    }




}

public Matrice(int numLignes, int numColonnes , double[] elements ) {
        this(numLignes,numColonnes);
        this.elements = new ArrayList<>() ;
        for(int i = 0 ;i<numLignes * numColonnes;i++){
            this.elements.add(elements[i]);

    }
}
public Matrice(Matrice autreMatrice) {
    numColonnes = autreMatrice.getNumColonnes();
    numLignes = autreMatrice.getNumLignes();

}

Is my shallow copy constructor is right ? Because when i run my teacher main test, its say im wrong. COuld be a error on my teacher main test, but i doubt it.

Thx !

2 Answers2

0

You need to copy the other matrix content as well not only the dimensions.

Zielu
  • 8,312
  • 4
  • 28
  • 41
-1

You could try doing this instead of getNumColonnes()

public Matrice(Matrice autreMatrice)
{
 autreMatrice.setNumColonnes(numColonnes);
 autreMatrice.setNumLignes(numLignes);
}

it should work if you have a setNumColonnes and setNumLines methods.

Steve
  • 19
  • 6