Home > database >  How to use the single if condition inside the super keyword?
How to use the single if condition inside the super keyword?

Time:03-08

I am trying to convert the below subclass constructor code to one line of code using the super constructor .This constructor only needs to verify that the 2D array has the same number of elements in both dimensions and pass that information to the super class constructor. My super class constructor has three parameters as given below. My subclass constructor has only one array parameter. I am attaching my super class constructor also here. In the square matrix class, I need to implement a class that represents a square matrix. I need to check the number of rows in the matrix must be the same as the number of columns. I must extend the Matrix class to implement my SquareMatrix class. I should not override any of the methods of the Matrix class, but I do need to create new constructors: public SquareMatrix(double [][] array2D) throws IllegalArgumentException My constructor initializes a new square matrix as an rc by rc matrix with values copies from array2D. My constructor should use the super class constructor to do all the work. If the super class constructor throws an exception, the SquareMatrix constructor need do nothing; the exception will simply propagate to the caller.

public class Matrix {
int nRows;
int nCols;
double arr[][];
public Matrix(int numRows,int numCols,double [][] array2D)throws IllegalArgumentException{
    this.nRows=numRows;
    this.nCols=numCols;

if(nRows!=array2D.length || nCols!=array2D[0].length) throw new IllegalArgumentException("Dimensions are not matched"); else {

        this.arr = new double[nRows][nCols];
        for(int i=0;i<this.nRows;i  ) {
            for(int j=0;j<this.nCols;j  ) {
                this.arr[i][j]=array2D[i][j];
            }
            
            }
        }
    } 

} public class SquareMatrix extends Matrix {

public SquareMatrix(double[][] array2D) throws IllegalArgumentException{
 
  if(array2D.length==array2D[0].length) {
      super(array2D.length,array2D[0].length,array2D);
  }                    

CodePudding user response:

The trick is simply to arrange that the super call can be in the first line of the subclass constructor.

public SquareMatrix(double[][] array2D){
  super(check(array2D), check(array2D), array2D);
}

private static int check(double[][] array2D) {
  if (array2D.length != array2D[0].length)
    throw new IllegalArgumentException("unsquare");
  return array2D.length;
} 

An exception is thrown since there seems to be no other way to construct the class correctly. It's "square or bust".

Yeah, we do the same computation twice. I didn't think it was worth working around, to save such a little.

However, there seems to be a simpler solution for this case:

public SquareMatrix(double[][] array2D){
  super(array2D.length, array2D[0].length, array2D);
  if (array2D.length != arrary2D[0].length)
    throw new IllegalArgumentException("unsquare");
}

There's no reason why the check for legitimacy has to be first.

Finally, the checking is flawed. It does not screen out ragged arrays. There's nothing stopping array2D[0].length != array2D[1].length.

CodePudding user response:

First, simplify. You don't need the numRows and numCols parameters if you're just going to get them from the actual data in the third parameter.

Then Create an abstract method in your base class called validate or something similar.

  private abstract void validate(double[][] data) throws MatrixValidationException

Then create a new Exception that can be thrown from your validate method, creatively enough, MatrixValidationException.

public class MatrixValidationException extends Exception {
   public MatricValidationException(String reason) {
      super(reason);
   }
}

If the matrix fails validation, have your subclass validate() method throw an exception with the details of why the validation failed.

private validate(double[][] data) throws MatrixValidatioException {
   if (data.length == 0) {
      throw new MatrixValidationException("No data");
   }
   if (data.length != data[0].length) {
      throw new MatrixValidationException("Matrix is not square.");
   }
}

Add a "throws MatrixValidationException" to your constructors

public Matrix(double[][] data) throws MatrixValidationException {
   validate(data);
   arr = data;
}

public SquareMatrix(double[][]data) throws MatrixValidationException {
    super(data);
}
  • Related