Issue with creating an object from a parameterized constructor with an array as a parameter

130 views Asked by At

I'm working on a Java project for a Yahtzee game and need to create a parametrized constructor that gives both of my instance variables their values. My two instance variables are arrays.

    public DiceArray(int[] die)
{
    die = new int[5];
    for( int i = 0; i < die.length; i++ )
    {
        die[i] = 0;
    }

    keep = new boolean[5];
    for( int i = 0; i < keep.length; i++ )
    {
        keep[i] = false;
    }       
}

When I try to create the object in my application class with

    // Testing parameterized constructor
    DiceArray myDice = new DiceArray();

I get Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor DiceArray() is undefined

When I take the parameter out of the method it works fine. Thanks in advance for any help

3

There are 3 answers

1
SMA On BEST ANSWER

You have not defined default constructor and you are trying to call default constructor, which is why compiler is not happy.

You need to use parameterized constructor in your case like:

int num[] = new int[5];
DiceArray myDice = new DiceArray(num);

And remove the num array initialization from your method as it's not advisable to modify the parameter's that you get in the method/constructor as it wont have any effect (i.e. it wont change anything in your array num that you defined just before calling constructor as above) on the calling method like we defined above.

In fact, i don't see you should be using constructor at all. (Assuming you have declared num and result array at object level already)As by default when you create int array, all the values are going to be 0 by default and for result i.e. boolean array, they will default to false.

So just remove the constructor and its body and it should work.

1
sunysen On

You have defined the constructor to take one parameter, but you are trying to call it with zero parameters. So, you can either remove the parameter from the constructor definition:

public DiceArray() {
  ...
}   

or you can use the parameter:

int[] num = new int[5];

DiceArray myDice = new DiceArray(num);
0
Master Slave On

What you tried,

DiceArray myDice = new DiceArray();

Is instatiating the myDice with a default constructor. From Wikipedia

In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor is also empty, meaning that it does nothing. A programmer-defined constructor that takes no parameters is also called a default constructor

When you create a parametrized constructor, the default one is not generated, and what sunysen is saying is either create a default constructor, or create your object by using the parametrized one.

Note another error, since you stated that die and keep are your instance variable, the die changes in the constructor will have no effect on the instance variable, since it is shadowed by the variable in the constructor of the same name. To fix this, prefix the this keyword

public DiceArray(int[] die)
{
    this.die = new int[5];
    ...
}