maven compiles all tests independetly of which tests are run
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package oving5.stringgrid;
|
||||
|
||||
/**
|
||||
* An interface with methods for managing the content of a String grid. The grid has a number of
|
||||
* rows (the grid's height) and columns (the grid's width). In each cell in the grid there is a
|
||||
* String that can be set with the setElement method and read with the getElement method.
|
||||
*/
|
||||
public interface StringGrid {
|
||||
|
||||
// Returns the number of rows in this StringGrid
|
||||
int getRowCount();
|
||||
|
||||
// Returns the number of columns in this StringGrid
|
||||
int getColumnCount();
|
||||
|
||||
// Returns the String at the given row and column. Throws an IllegalArgumentException if the
|
||||
// row or column is out of range
|
||||
String getElement(int row, int column);
|
||||
|
||||
// Sets the String at the given row and column. Throws an IllegalArgumentException if the row
|
||||
// or column is out of range
|
||||
void setElement(int row, int column, String element);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package oving5.stringgrid;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
public class StringGridImpl implements StringGrid {
|
||||
private ArrayList<String> grid;
|
||||
private int rows;
|
||||
private int columns;
|
||||
|
||||
StringGridImpl(int rows, int columns) {
|
||||
grid = new ArrayList<String>(Collections.nCopies(rows * columns, ""));
|
||||
this.rows = rows;
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public int getRowCount() {
|
||||
return rows;
|
||||
}
|
||||
|
||||
public int getColumnCount() {
|
||||
return columns;
|
||||
}
|
||||
|
||||
public String getElement(int row, int column) {
|
||||
if (row < 0 || row >= rows || column < 0 || column >= columns) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid value for row or column, got row: " + row + ", and column: " + column);
|
||||
}
|
||||
return grid.get(row * columns + column);
|
||||
}
|
||||
|
||||
public void setElement(int row, int column, String element) {
|
||||
if (row < 0 || row >= rows || column < 0 || column >= columns) {
|
||||
throw new IllegalArgumentException(
|
||||
"Invalid value for row or column, got row: " + row + ", and column: " + column);
|
||||
}
|
||||
grid.set(row * columns + column, element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String r = "";
|
||||
for (int y = 0; y < rows; y++) {
|
||||
for (int x = 0; x < columns; x++) {
|
||||
r += grid.get(y * columns + x);
|
||||
if (x < columns - 1) {
|
||||
r += " | ";
|
||||
}
|
||||
}
|
||||
r += "\n";
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package oving5.stringgrid;
|
||||
|
||||
public class StringGridIterator {
|
||||
private StringGrid grid;
|
||||
private boolean rowMajor;
|
||||
private int i = -1;
|
||||
|
||||
public StringGridIterator(StringGrid grid, boolean rowMajor) {
|
||||
this.grid = grid;
|
||||
this.rowMajor = rowMajor;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return (i + 1) < (grid.getRowCount() * grid.getColumnCount());
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String next() {
|
||||
if (hasNext()) {
|
||||
i += 1;
|
||||
if (rowMajor) {
|
||||
return grid.getElement(i / grid.getColumnCount(), i % grid.getColumnCount());
|
||||
} else {
|
||||
return grid.getElement(i % grid.getRowCount(), i / grid.getRowCount());
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user