0

The question is if x >=1
then i creates the array
if not then it defaults x to 1.

Zoo has one constructor which takes an int as a parameter and uses it to instantiate the array
animals to that size, and it also sets count to zero. It should make sure the parameter is great
than or equal to 1 before using it to create the array. If invalid simply use size 1 for default.

I have this

public Zoo(int n)
{
   if(n.length >= 1)
   {
     return animals = new int[n] animal;
   else
     n = 1;
   }

   count = 0;

}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by ~s.o.s~
0

`n` is an integer (primitive type) and doesn't have a `length` property/attribute. You just need to check whether the passed in number (probably the number of animals in a Zoo) is a valid number; if not, use 1 as the default.

class Animal {}

class Zoo {
  private Animal[] animals;

  public Zoo(int n) {
    // check if `n` is invalid; if it is, reassign & make it 1
    this.animals = new Animal[n];
  }

}

Give it a shot.

EDIT: Also, you don't need a *return* statement in a constructor. You might want to read this once again.

Edited by ~s.o.s~: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.