javaconstructoroverloadingminmultipleoutputs

Overloading constructor of a class and each constructor will have different instance variables


I initially wanted to create a "method" Min, that will search the min of a an array and gives back two elements. One will represent the minimum value and the other the index of this value.In order to do that, I decided to create a class Min and a constructor with those two elements stored as instance variables accessible with getter and setter (a double and an int).

The tricky part is that then, I wanted to overload my class, and add a second constructor, this one will work with a 2D array as input and have two other instances variables ( a double[] and an int[]).

It is working, however I am uncomfortable, as I could create instance of this class for example with the first constructor, AND still have access to the two other variables that have no meaning to this constructor and vice versa.

I could have created a totally other class for my second constructor to solve this issue (ex: one class Min_array, and Min_2Darray), however I would like them to have the same name as it represent the same kind of operation.

I believe there should be a more elegant way for overloading and retrieve multiple results than the one I choose, I would be glad of some advices or to know what are your best practices. Thank you.

My second (more small) concern is that I had to create a lot of instances of constructor 1 in constructor 2 which seems to be quite odd and not efficient at all in term of memory allocation.

My class Min :

package operation;

public class Min {

    // ---------------- Instance Variables ------------------ 
    private int index = 0; 
    private double value = Double.POSITIVE_INFINITY; // important as i search for the MIN 
    private int[] ArrayIndex;   
    private double[] ArrayValue; 


    // ---------------- Constructor 01 ------------------ 
    public Min(double [] anArray) {
        for (int i = 0; i < anArray.length; i++) {
            if (anArray[i] < this.value) {
                this.value = anArray[i]; 
                this.index = i; 
            }
        }
    }

    // ---------------- Constructor 02 ------------------ 
    public Min(double [][] a2DArray, boolean accordingToRow) {

        int n_row = a2DArray.length; 
        int n_col = a2DArray[0].length; 

        if (accordingToRow == true) {
            this.ArrayIndex = new int [n_row]; 
            this.ArrayValue = new double [n_row];
            for (int i = 0; i < n_row; i++) {
                Min minofOneArray = new Min(a2DArray[i]);  // Here i call and create multiple instance of constructor 01.  
                this.ArrayIndex[i] = minofOneArray.getIndex(); 
                this.ArrayValue[i] = minofOneArray.getValue(); 
            }

        }else { // accordingToRow == false (so it according to Column now)
            
            this.ArrayIndex = new int [n_col]; 
            this.ArrayValue = new double [n_col]; 

            //need to loop to extract a column in this case
            double[] tmpArray = new double [n_row]; 

            for (int j = 0; j < n_col; j++) {
                for (int i = 0; i < n_row; i++) {
                    tmpArray[i] = a2DArray[i][j]; 
                }
                Min minofOneArray = new Min(tmpArray); 
                this.ArrayIndex[j] = minofOneArray.getIndex(); 
                this.ArrayValue[j] = minofOneArray.getValue(); 
            }

        }
        
    }

    // ---------------- Getters & Setters ------------------ 
    public int getIndex() {return this.index ; }
    public double getValue() {return this.value ; }

    public int[] getArrayIndex() {return this.ArrayIndex ; }
    public double[] getArrayValue() {return this.ArrayValue ; }

}

The test of my class Min :

package operation_test;

import java.util.Arrays;

import operation.Min;

class Test_MIN {
public static void main(String[] args) {
        
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>> Test of the Min Class <<<<<<<<<<<<<<<<<<<<<<<<<<" );

        
        double [] anArray = { 0,  2 , 1 , -2 , 5 };  
        
        Min minOfArray = new Min(anArray); 
        
        System.out.println(minOfArray.getIndex());
        System.out.println(minOfArray.getValue());
        System.out.println("--------------- End of test 01 -----------------" );
        
        double [][] a2DArray = {{0.2,5,-1},{1,3,0.5}};  
        
        // 0  5 -1
        // 1  3 0.5
        
        
        Min minOf2DArray = new Min(a2DArray, true); // according to row 
       
        System.out.println(Arrays.toString(minOf2DArray.getArrayIndex()));
        System.out.println(Arrays.toString(minOf2DArray.getArrayValue()));
        System.out.println("--------------- End of test 02 -----------------" );
        
        Min minOf2DArray_AccordingToCol = new Min(a2DArray, false); // according to column
           
        System.out.println(Arrays.toString(minOf2DArray_AccordingToCol.getArrayIndex()));
        System.out.println(Arrays.toString(minOf2DArray_AccordingToCol.getArrayValue()));
        System.out.println("--------------- End of test 03 -----------------" );
        

    }
}

The results of my class Min :

>>>>>>>>>>>>>>>>>>>>>>>> Test of the Min Class <<<<<<<<<<<<<<<<<<<<<<<<<<
3
-2.0
--------------- End of test 01 -----------------
[2, 2]
[-1.0, 0.5]
--------------- End of test 02 -----------------
[0, 1, 0]
[0.2, 3.0, -1.0]
--------------- End of test 03 -----------------

Solution

  • If you want your different constructors to setup instances with different sets of instance variables, then you want to create instances of different classes. Those can inherit a base class with the common parts.

    (This, though very short, has been perceived as an answer by OP and @user. I create it to get the question out of the list of unanswered questions. Let me know when a more helpful or technically detailed answer is created. I don't mind deleting mine then.)