Comment écrire une methode readMatrix() contenue en JAVA

sylashezou Messages postés 2 Date d'inscription mardi 10 août 2010 Statut Membre Dernière intervention 20 juin 2012 - 15 août 2010 à 03:41
Kidator Messages postés 16 Date d'inscription dimanche 6 juin 2010 Statut Membre Dernière intervention 31 août 2010 - 20 août 2010 à 22:50
Bonjour,je suis étudiant débutant en Java.On me demande dans un programme d'écrire une méthode static readMatrix() demandant à l'utilisateur les dimensions de la matrice ainsi que les coefficient et qui retourne une instance de matrice.comment écrire cette méthode?pouvez-vous m'aider s'il vous plait?

sylashezou

3 réponses

Kidator Messages postés 16 Date d'inscription dimanche 6 juin 2010 Statut Membre Dernière intervention 31 août 2010
16 août 2010 à 05:01
Salut, une ébauche d'idée:

J'espère que ça t'aidera! c soit ça soit avec GUI

//je suppose les coefficients entiers
/**
* Objet Matrix représente une matrice
*/
class Matrix {

int dimensionX, dimensionY;
int[][] coefficients;

/**
* crée une instance (object) Matrix (matrice)
*/
public Matrix(int dimensionX, int dimensionY, int[][] coefficients){
this.int dimensionX = dimensionX;
this.dimensionY = dimensionY;
this.coefficients = coefficients
}

}


public static Matrice readMatrix(String[] args){
int dimensionX Interger.ParseInt(args[0]), dimensionY Interger.ParseInt(args[1]);
int[][] coefficients;

for(int i = 0; i < dimensionX;i++){
for(int j = 0;j < dimensionY;j++){
coefficients [i][j] = Interger.ParseInt(args[i * dimensionY + j]);
}
}


return new Matrix(dimensionX,
dimensionY,
coefficients);
}

/**Les données sont entrées en arguments
* au lancement du programme
* il faut bien sur ajouter des tests
* pour vérifier que les entrées sont
* correctes!
*/
public static void main(String[] args){
readMatrix(args);
}
0
Kidator Messages postés 16 Date d'inscription dimanche 6 juin 2010 Statut Membre Dernière intervention 31 août 2010
16 août 2010 à 22:02
une petite correction :

remplacer:
int dimensionX, dimensionY;
int[][] coefficients;
.
.
.
coefficients [i][j] = Interger.ParseInt(args[i * dimensionY + j]);


par
int dimensionX, dimensionY;
int[][] coefficients;
int coefficientCount = 2;
.
.
.
coefficients [i][j] = Interger.ParseInt(args[coefficientCount++]);
0
Kidator Messages postés 16 Date d'inscription dimanche 6 juin 2010 Statut Membre Dernière intervention 31 août 2010
20 août 2010 à 22:50
Salut, cela devrait être mieux :

/**
*
* @author Kidator & Cie
*/
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
Rejoignez-nous