Comment écrire la methode readMatrix en java

Fermé
hezousylvain - Modifié par hezousylvain le 14/08/2010 à 01:18
 java - 15 févr. 2011 à 15:46
Bonjour,
je suis étudiant debutant en java. J'ai écrit un programme dans lequel on me demande de créer une classe Matrix() dans laquelle on me demande de créer les methodes getValue(int i, int j),setValue(int i,int j,double val) permettant d'acceder et de modifier la valeur du coefficient i,j de la matrice.
Ensuite de définir les methodes getLines, getColumns, toString() de Matrix.
voici le programme:


public class Matrix {
double [][]coeff;
public Matrix(double [][]a){
this.coeff=a;
}
public double getValue(int i,int j){
return this.coeff[i][j];
}
public void setValue(int i,int j,double val){
this.coeff[i][j]=val;
}
public int getColumns(){
return this.coeff.length;
}
public int getLines(){
return this.coeff[0].length;
}
public String toString(){
String out="";
int i,j;
for (i=0; i<this.getLines(); i++){

for(j=0; j<this.getColumns(); j++)
out+=this.coeff[i][j]+"\t";
out+="\n";
}


return out; }
}
ensuite on me demande de définir une classe TestMatrrix contenant une methode main.Puis dans cette classe TestMatrix d'écrire une methode static readMatrix() qui demande à l'utilisateur les dimensions de sa matrice et ses coefficients et retourne une instance de matrice.
Comment écrire cette méthode readMatrix?Pouvez-vous m'aider?
Sylvain





A voir également:

2 réponses

Salut, j'espère que ceci t'aidera malgré le retard :


/**
*
* @author hezousylvain & Kidator
*/
public class TestMatrix {
public class Matrix {
double [][]coeff;
public Matrix(double [][]a){
this.coeff=a;
}
public double getValue(int i,int j){
return this.coeff[i][j];
}
public void setValue(int i,int j,double val){
this.coeff[i][j]=val;
}
public int getColumns(){
return this.coeff[0].length;
}
public int getLines(){
return this.coeff.length;
}
public String toString(){
String out="";
int i,j;
for (i=0; i<this.getLines(); i++){

for(j=0; j<this.getColumns(); j++)
out+=this.coeff[i][j]+"\t";
out+="\n";
}


return out; }
}

public static Matrix readMatrix(String[] args, Matrix matrix){
matrix.coeff = new double[Integer.parseInt(args[0])][Integer.parseInt(args[1])];

for(int i = 0; i < matrix.getLines();i++){
for(int j = 0;j < matrix.getColumns();j++){
matrix.setValue(i, j, Double.parseDouble(args[i * matrix.getColumns() + j + 2]));
}
}

return matrix;
}

/**
* Creates a new instance of TestMatrix
*/
public TestMatrix(String[] args) {
Matrix matrix = readMatrix(args, new Matrix(null));
}

/**
* Les données sont entrées en arguments
* au lancement du programme
*/
public static void main(String[] args){
new TestMatrix(args);
}

}

Kidator.
0
Bonjour
Je n'ai pas lu en profondeur votre code, mais pour simplement la méthode toString(), vous deviez aller à la ligne dans le premier for et non le deuxième. cela permet un bon affichage.
0