Home > Blockchain >  Why doesn't the second constructor take in a first constructor's object as input?
Why doesn't the second constructor take in a first constructor's object as input?

Time:12-05

The problem:

Why doesn't the second constructor take in a first constructor's object as input?

And in a more general overview of the code, if you guys spot any problematic logic issues I'd be really thankful if you can point them out.

import java.util.Scanner;

    public class Square3x3{
        private static final int DEF_VALUE = -1;
        private static final int ROW_COUNT = 3; 
        private static final int COL_COUNT = 3;
        private static final int LOWER_BOUND = 0;
        
        private int [][] _square;
        
        public Square3x3(){
        _square = new int [ROW_COUNT][COL_COUNT];
            for(int i = 0 ; i < ROW_COUNT ; i  )
            {
                for (int j = 0; j < COL_COUNT; j  )
            {
                _square[i][j] = DEF_VALUE;
                System.out.print(_square[i][j]);
            }
            System.out.println();
        }
        }
        public Square3x3(int[][]array){
            int rows = array.length;
            int column;
            int colCount = 0;
            
            for(int j = 0; j < rows; j  ){
                    colCount = j;
            }
            column = colCount;
            if (rows > ROW_COUNT || column > COL_COUNT){
                for (int k = 0; k < ROW_COUNT; k  )
                    for (int g = 0 ; g < COL_COUNT; g  ){
                        _square[k][g] = array[k][g];
                    }
            }
            if (rows < 3 || column < 3){
                for (int x = 0; x > array.length; x  )
                    for (int y = 0 ; y > array.length; y  ){
                         array[x][y] = -1;
                        }
                        
                    }
            if (rows == 3 && column == 3){
                for (int i = 0; i < array.length; i  ){
                    for (int j = 0; j < array.length; j  ){
                        _square[i][j] = array[i][j];
                    }
                    }
                }
                
            }    
        public Square3x3(Square3x3 other){
            if (other != null){
                other = new Square3x3();
                for (int i = 0; i < ROW_COUNT; i  )
                    for (int j = 0; j < COL_COUNT; j  ){
                        this._square[i][j]=other._square[i][j];
                    }
            }
        }  

CodePudding user response:

Not really sure if I understood your question but it seems to me that you have an issue with the constructor public Square3x3(Square3x3 other). You are reseting the other object to a brand new Square3x3 in the line other = new Square3x3(); before copying its contents. What you want is the following:

public Square3x3(Square3x3 other) {
    this();
    if (other != null) {
        for (int i = 0; i < ROW_COUNT; i  ) {
            for (int j = 0; j < COL_COUNT; j  ) {
                this._square[i][j] = other._square[i][j];
            }
        }
    }
}

Or, if you want to make it simpler by using System.arraycopy():

public Square3x3(Square3x3 other) {
    this();
    if (other != null) {
        for (int i = 0; i < ROW_COUNT; i  ) {
            System.arraycopy(other._square[i], 0, this._square[i], 0, COL_COUNT);
        }
    }
}

I am still confused about what you really want to achieve with your question, but if you instantiate a Square3x3 object and then for whatever reason you want to instantiate a new one by calling the public Square3x3(int[][] array) constructor then you need to expose a getter to access _square property contents (consider renaming it to square):

public class Square3x3 {
    (...)

    public int[][] get_square() {
        return _square;
    }

    (...)
}

And then you can do the following:

public class Main {
    public static void main(String[] args) {
        Square3x3 square = new Square3x3();
        Square3x3 newSquare = new Square3x3(square.get_square());
    }
}

CodePudding user response:

There was a similar question a few weeks ago about implementing the second constructor, but it seems here more details about a default and copy constructor have been added, so the solution should be modified.

Why doesn't the second constructor take in a first constructor's object as input?

Because the input array of the second constructor needs to be copied to the field square3x3. So the second constructor should invoke the default one this() and after that copy the contents of the input array as specified.

It's worth to note that usually the parameterless constructor invokes the constructor with argument and provides a default value but this is definitely not that case because this would result in duplicate creation of the array in these two constructors.

So, the first(default) constructor creates and populates the array field. The second constructor deals with the copying the data from the input array. The copy (third) constructor invokes the 2nd:

public class Square3x3 {
    private int[][] square3x3;

    public Square3x3() {
        this.square3x3 = new int[][] {
           {-1, -1, -1},
           {-1, -1, -1},
           {-1, -1, -1}
        };
    }

    public Square3x3(int[][] array) {
        this();
        for (int i = 0, n = Math.min(3, array == null ? -1 : array.length); i < n; i  ) {
            for(int j = 0, m = Math.min(3, array[i] == null ? -1 : array[i].length); j < m; j  ) {
                square3x3[i][j] = array[i][j];
            }
        }
    }

    public Square3x3(Square3x3 other) {
        this(null == other ? null : other.square3x3);
    }

    public int[][] getSquare() {
        return square3x3;
    }
}

Also, initialization may be moved out of the default constructor and then it could be empty, and the 2nd constructor does not need to invoke it. This saves a few lines of code but does not fully meet the specified requirements.

public class Square3x3 {
    private int[][] square3x3 = {
       {-1, -1, -1},
       {-1, -1, -1},
       {-1, -1, -1}
    };

    public Square3x3() {
        // empty, provided just for invocation
    }

    public Square3x3(int[][] array) {
        for (int i = 0, n = Math.min(3, array == null ? -1 : array.length); i < n; i  ) {
            for(int j = 0, m = Math.min(3, array[i] == null ? -1 : array[i].length); j < m; j  ) {
                square3x3[i][j] = array[i][j];
            }
        }
    }

    public Square3x3(Square3x3 other) {
        this(null == other ? null : other.square3x3);
    }
// ...
}

Tests:

Square3x3 def = new Square3x3();
Square3x3 copy = new Square3x3((Square3x3) null); // casting needed to resolve ambiguity
Square3x3 arr = new Square3x3(new int[][]{{1, 2, 3, 4}, null, {-10}, {5, 6}});

System.out.println(Arrays.deepToString(def.getSquare()));
System.out.println(Arrays.deepToString(copy.getSquare()));
System.out.println(Arrays.deepToString(arr.getSquare()));

Output:

[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
[[1, 2, 3], [-1, -1, -1], [-10, -1, -1]]
  • Related