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

avancée

parent 9d6561d6
Branches
No related tags found
No related merge requests found
Pipeline #38386 failed
...@@ -2,15 +2,15 @@ package matrix; ...@@ -2,15 +2,15 @@ package matrix;
public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> { public class ConstantMatrixInitializer<T> implements MatrixInitializer<T> {
// TODO: add instance variables private T constant ;
public ConstantMatrixInitializer(T constant) { public ConstantMatrixInitializer(T constant) {
// TODO this.constant = constant;
} }
@Override @Override
public T initialValueAt(Coordinate coordinate) { public T initialValueAt(Coordinate coordinate) {
// TODO
return null; return constant;
} }
} }
package matrix; package matrix;
import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -23,10 +24,9 @@ public class ListMatrix<T> implements Matrix<T> { ...@@ -23,10 +24,9 @@ public class ListMatrix<T> implements Matrix<T> {
* @param initializer A matrix initializer to set values in the {@link ListMatrix}. * @param initializer A matrix initializer to set values in the {@link ListMatrix}.
*/ */
public ListMatrix(int width, int height, MatrixInitializer<T> initializer) { public ListMatrix(int width, int height, MatrixInitializer<T> initializer) {
// TODO this.width = width;
this.width = 0; this.height = height;
this.height = 0; this.matrix = new ArrayList<>(width);
this.matrix = null;
this.initializeWith(initializer); // fills the matrix using initializer this.initializeWith(initializer); // fills the matrix using initializer
} }
...@@ -35,9 +35,18 @@ public class ListMatrix<T> implements Matrix<T> { ...@@ -35,9 +35,18 @@ public class ListMatrix<T> implements Matrix<T> {
} }
private void initializeWith(MatrixInitializer<T> initializer) { 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() { public int width() {
return width; return width;
} }
...@@ -48,14 +57,14 @@ public class ListMatrix<T> implements Matrix<T> { ...@@ -48,14 +57,14 @@ public class ListMatrix<T> implements Matrix<T> {
@Override @Override
public T get(int x, int y) { public T get(int x, int y) {
// TODO
return null; return matrix.get(x).get(y);
} }
@Override @Override
public void set(int x, int y, T newValue) { 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> { ...@@ -58,7 +58,7 @@ public interface Matrix<T> extends Iterable<T> {
} }
default Matrix<T> subMatrix(Coordinate corner, int width, int height){ 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