Skip to content
Snippets Groups Projects
Commit 87a27dec authored by hiba1907's avatar hiba1907
Browse files

fin des parties du tp6

parent 680b3481
No related branches found
No related tags found
No related merge requests found
Pipeline #41132 failed
Showing
with 38 additions and 16 deletions
File added
No preview for this file type
File added
No preview for this file type
......@@ -40,7 +40,7 @@ public class ListMatrix<T> implements Matrix<T> {
for (int y =0; y<height ;y++){
List<T> row =new ArrayList<>(width);
for(int x=0; x<width; x++){
row.add(initializer.initialValueAt((new Coordinate(x, y))));
row.add(initializer.initialValueAt(new Coordinate(x, y)));
}
matrix.add(row);
}
......@@ -57,18 +57,15 @@ public class ListMatrix<T> implements Matrix<T> {
@Override
public T get(int x, int y) {
if(x<0 || x>= width || y<0 ||y>=height ){
throw new IndexOutOfBoundsException("Invalid coordinate: ("+ x+","+y+")");
}
return matrix.get(y).get(x);
}
@Override
public void set(int x, int y, T newValue) {
if (x<0 || x>= width ||y<0 || y>= height){
throw new IndexOutOfBoundsException("Invalid coordinate: ("+ x + ","+y+")");
}
matrix.get(y).set(x, newValue);
}
......
......@@ -58,7 +58,7 @@ public interface Matrix<T> extends Iterable<T> {
}
default Matrix<T> subMatrix(Coordinate corner, int width, int height){
return null ;
return new ListMatrix<>(width, height, new SubMatrixInitializer<>(this, corner.x(),corner.y()));
}
......
......@@ -15,4 +15,7 @@ public interface MatrixInitializer<T> {
* @return The initial value for the specified cell.
*/
T initialValueAt(Coordinate coordinate);
}
package matrix;
public class SubMatrixInitializer<T> implements MatrixInitializer<T> {
private final Matrix<T> originMatrix;
private final int x0;
private final int y0;
public SubMatrixInitializer(Matrix<T> originMatrix,int x0, int y0){
this.originMatrix=originMatrix;
this.x0=x0;
this.y0=y0;
}
@Override
public T initialValueAt(Coordinate coordinate){
int x= coordinate.x()+x0;
int y = coordinate.y()+y0;
return originMatrix.get(x,y);
}
}
......@@ -11,7 +11,7 @@ import java.util.List;
*/
public class Cell<T> implements Lens<T> {
//TODO: ajouter la ou les propriétés nécessaires
private T value;
// la liste des objets écoutant les modifications du contenu de la cellule
private final List<OnChangeListener<T>> listeners = new ArrayList<>();
......@@ -21,7 +21,7 @@ public class Cell<T> implements Lens<T> {
* @param initialContent the value initially stored by the cell.
*/
public Cell(T initialContent) {
//TODO: à compléter
this.value= initialContent;
}
/** Add a {@link OnChangeListener} to react to any change of value in the cell.
......@@ -40,8 +40,10 @@ public class Cell<T> implements Lens<T> {
* @param value the new content of this {@link Cell}
*/
public void set(T value) {
//TODO: modifier le contenu de la cellule, puis appeler les méthodes valueChanged des
// listeners
this.value=value;
for (OnChangeListener<T> listener :listeners){
listener.valueChanged(value,value);
}
}
/**
......@@ -50,7 +52,6 @@ public class Cell<T> implements Lens<T> {
* @return the current content of this {@link Cell}
*/
public T get(){
//TODO: à compléter
return null;
return value;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment