Skip to content
Snippets Groups Projects
Commit 6a93814e authored by BALME Maxence's avatar BALME Maxence
Browse files

avancée

parent 9d6561d6
No related branches found
No related tags found
No related merge requests found
Pipeline #38386 failed
......@@ -2,15 +2,15 @@ package matrix;
public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> {
// TODO: add instance variables
private T constant ;
public ConstantMatrixInitializer(T constant) {
// TODO
this.constant = constant;
}
@Override
public T initialValueAt(Coordinate coordinate) {
// TODO
return null;
return constant;
}
}
package matrix;
import java.util.ArrayList;
import java.util.List;
......@@ -23,10 +24,9 @@ public class ListMatrix<T> implements Matrix<T> {
* @param initializer A matrix initializer to set values in the {@link ListMatrix}.
*/
public ListMatrix(int width, int height, MatrixInitializer<T> initializer) {
// TODO
this.width = 0;
this.height = 0;
this.matrix = null;
this.width = width;
this.height = height;
this.matrix = new ArrayList<>(width);
this.initializeWith(initializer); // fills the matrix using initializer
}
......@@ -35,9 +35,18 @@ public class ListMatrix<T> implements Matrix<T> {
}
private void initializeWith(MatrixInitializer<T> initializer) {
// TODO initialize each cell of the matrix, with a value determined by initializer
for (int x=0; x<width; x++) {
List<T> column = new ArrayList<>(height);
for (int y = 0; y < height; y++) {
column.add(initializer.initialValueAt(new Coordinate(x, y)));
}
matrix.add(column);
}
}
public int width() {
return width;
}
......@@ -48,14 +57,14 @@ public class ListMatrix<T> implements Matrix<T> {
@Override
public T get(int x, int y) {
// TODO
return null;
return matrix.get(x).get(y);
}
@Override
public void set(int x, int y, T newValue) {
// TODO
matrix.get(x).set(y,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));
}
......
package matrix;
public class SubMatrixInitializer <T> implements MatrixInitializer<T> {
private final Matrix <T> matrix;
private final Coordinate corner ;
public SubMatrixInitializer(Matrix<T> matrix, Coordinate corner) {
this.matrix = matrix;
this.corner = corner;
}
@Override
public T initalValueAt(Coordinate coordinate) {
return matrix.get(coordinate.plus(corner));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment