0

I'm writing my firsts classes in java.

I did a class with an int[] that must be declared with 3 elements.

public abstract class MyClass {
   private String name;
   private int[] myValues = new int[2];

now my question is:

  1. what is the best way to ensure that user must insert every member?

Then.. The constructor would be the best way to do that, so I did the constructor with every single element clearly required:

public MyClass(String nam, int val0, int val1, int val2){
   int[] values = new int[]{val0, val1, val2};
   setMyValues(values);
}

private void setMyValues(int[] vals){
   this.myValues = vals;
}
  1. it's a good practice? seems to be complicated instead of giving a simple array as:

    public void MyClass(int[] vals)..
    

    but in this way I can't be sure about number of elements.I should create a custom-exception or an if(vals.length != 2) cicle..

I've some other doubts:

  1. is useful to declare myValues = new int[2] or it's the same simply writing int[] myValues? (why declare number of elements in the inner state?)

  2. it's better to receive parameters from constructor or pass a vector to setter (setMyValues(int[] vals)) and check array in setter?

tank you for every suggestion.

Marco
  • 133
  • 2
  • 13

2 Answers2

1
  1. You could have a overloaded constructor, one with the three integer values and one with the array. On the first one build an array and pass it to the array constructor, inside the second constructor call the setter and evaluate the length, if is longer than three throw the exception.
  2. You're allocating an object which is never used, wastes space and is going to be collected by the GC after the new array is assigned. I'd assign nothing to that property.
  3. It depends upon your approach. You can go for the first point and add a third default constructor, then set the array using the setter method.

Hope this help.

E. Mancebo
  • 629
  • 3
  • 10
1
public class MyClass {

    private String name;
    private int[] myvalues = new int[3]; 
    //you want the array to hold 3 elements so the size should be 3

    //constructor
    public MyClass(String name) {
        this.name = name; // each instance is defined by a name 

        System.out.println("enter 3 values, each on a line");
//3
//4
//5
        for(int i = 0; i < 3 ;i++) {// loop restricts the user to 3 elements
            Scanner console = new Scanner(System.in); // import Scanner
            this.myvalues[i] = console.nextInt();
        }
//myvalues = {3,4,5}

    }

    public static void main(String[] args) {

        MyClass C = new MyClass("Points of triangle");
    }
}

you need to declare the size of the array to avoid null pointer Exception. this way you can declare different objects each with array elements from the user

note if you want the user to enter them on the same line

    System.out.println("enter 3 values on a line separated by space");
//3 4 5

    Scanner console = new Scanner(System.in); // import Scanner
    for (int i = 0; i <3 ; i++) {
        this.myvalues[i] = console.nextInt();
    }
}
//myvalues = {3,4,5}
TheCures
  • 11
  • 1
  • "//you want the array to hold 3 elements so the size should be 3" if I have an array: int[2] => i[0], i[1], i[2] (are not 3 elemnts?) – Marco Nov 08 '17 at 22:20
  • 1
    int[size] => i[0],i[1],i[2],...,i[size-1]. Hence int[2] => i[0], i[1] – TheCures Nov 09 '17 at 00:32