javainheritanceacm-java-libraries

How can I expand things from a superclass? (Java)


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);      
    }
  }

Solution

  • 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?

    You could create your own exception to handle the error case :

    public class IncorectValueException extends Exception{
      public IncorectValueException(){
        super();
      }
    }
    

    Besides, in NumberBoard class, in setValueAt() 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 :

    `isInRange(row, col) && (value == EMPTY || value >= 0)`
    

    could be a new method : isAcceptableValue(int row, int col, int value)

    Change it :

    public void setValueAt(int row, int col, int value) throws IncorectValueException{
        if (isInRange(row, col) && (value == EMPTY || value >= 0)) {
            board[row][col] = value;
        }
    }
    

    to :

    public void setValueAt(int row, int col, int value) throws IncorectValueException{
        if (!isAcceptableValue(row, col)) {
           throw new IncorectValueException();             
        }
          board[row][col] = value;
    }
    
    public boolean isAcceptableValue(int row, int col, int value){
       if(isInRange(row, col) && (value == EMPTY || value >= 0)){
         return true;
       }
       return false;
    }
    

    Now, the Sudoku class could be like that :

    public class Sudoku extends NumberBoard {
         ...
    public boolean isAcceptableValue(int row, int col, int value){ 
       if(isInRange(row, col) && (value==EMPTY || (value >= 1 && value <= 9))) {
          return true;             
        }       
        return false;
    }