td/tp semaines 1-2
This commit is contained in:
85
TP/ClassesDeDepart/MatriceCarre.java
Normal file
85
TP/ClassesDeDepart/MatriceCarre.java
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Une classe définissant une matrice carrée
|
||||
* @author Luc Dartois
|
||||
* @version 1.0
|
||||
*/
|
||||
public class MatriceCarre{
|
||||
private int n; //taille de la matrice
|
||||
private int[][] m; //La matrice
|
||||
|
||||
/**
|
||||
* Constructeur créant une matrice vide
|
||||
*@param t La taille de la matrice
|
||||
*/
|
||||
public MatriceCarre(int t){
|
||||
this.n=t;
|
||||
this.m=new int[t][t];
|
||||
}
|
||||
/**
|
||||
* Constructeur copiant une matrice donnée dans une nouvelle matrice
|
||||
*@param a La matrice à copier
|
||||
*/
|
||||
public MatriceCarre(MatriceCarre a){
|
||||
this.n=a.n;
|
||||
this.m=new int[this.n][this.n];
|
||||
for(int i=0;i<this.n;i++){
|
||||
for(int j=0;j<this.n;j++){
|
||||
this.m[i][j]=a.m[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Méthode permettant de définir le coefficient d'une matrice
|
||||
*@param i Le coefficient de ligne
|
||||
*@param j Le coefficient de colonne
|
||||
*@param val La valeur du coefficient
|
||||
*/
|
||||
public void setCoeff(int i,int j,int val){
|
||||
if(i>=this.n||j>=this.n||i<0||j<0){
|
||||
throw new IllegalArgumentException("Erreur : Hors limites de la matrice.");
|
||||
}
|
||||
this.m[i][j]=val;
|
||||
}
|
||||
/**
|
||||
* Méthode permettant de récuperer le coefficient d'une matrice
|
||||
*@param i Le coefficient de ligne
|
||||
*@param j Le coefficient de colonne
|
||||
*@return Le coefficient
|
||||
*/
|
||||
public int getCoeff(int i,int j){
|
||||
if(i>=this.n||j>=this.n||i<0||j<0){
|
||||
throw new IllegalArgumentException("Erreur : Hors limites de la matrice.");
|
||||
}
|
||||
return this.m[i][j];
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
String s=" ";
|
||||
for(int j=0;j<this.n;j++){
|
||||
s+="| "+j;
|
||||
}
|
||||
s+="|\n";
|
||||
for(int i=0;i<this.n;i++){
|
||||
s+=i+" | ";
|
||||
for(int j=0;j<this.n;j++){
|
||||
s+=this.m[i][j]+"| ";
|
||||
}
|
||||
s+="\n";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public boolean equals(MatriceCarre a){
|
||||
if(this.n!=a.n)
|
||||
return false;
|
||||
|
||||
for(int i=0;i<this.n;i++){
|
||||
for(int j=0;j<this.n;j++){
|
||||
if(this.m[i][j]!=a.m[i][j])
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user