I want to expand the method setValueAt(int row, int col, int value) from the superclass NumberBoard in my subclass Sudoku.
In Sudoku the value is empty or 1 to 9, but in the superclass NumberBoard the value may be empty or >= 0. How can I change it in my subclass Sudoku?
The superclass NumberBoard (I can't change the superclass):
public class NumberBoard {
/** Value of an empty cell. */
public static final int EMPTY = -1;
/** The board's state. */
private int[][] board;
/**
* Sets the value of the given cell.
*
* @param row
* the cell's row, starting at 0.
* @param col
* the cell's column, starting at 0.
* @param value
* the cell's value. Must be {@code >= 0} or {@link #EMPTY}.
*/
public void setValueAt(int row, int col, int value) {
if (isInRange(row, col) && (value == EMPTY || value >= 0)) {
board[row][col] = value;
}
}
/**
* Checks if the given coordinates identify a valid cell on the board.
*
* @param row
* the cell's row, starting at 0.
* @param col
* the cell's column, starting at 0.
* @return {@code true} if the coordinate is in range, {@code false}
* Â otherwise.
*/
protected final boolean isInRange(int row, int col) {
return 0 <= row
&& row < board.length
&& 0 <= col
&& col < board[row].length;
}
}
And my Code for my subclass Sudoku (Unfortunately without a point) :
public class Sudoku extends NumberBoard {
public void setValueAt(int row, int col, int value) {
super.setValueAt(row, col, value);
}
}
You could create your own exception to handle the error case :
Besides, in
NumberBoardclass, insetValueAt()method you perform too many unitary processings which should be grouped in a specific method if you want to redefine the behavior of checking valid data according to the class :could be a new method :
isAcceptableValue(int row, int col, int value)Change it :
to :
Now, the
Sudokuclass could be like that :