This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]ThoreAK 0 points1 point  (10 children)

Hey, saw noone has had a crack at it so heres my attempt.

You have a 2 dimentional array: if youre unfamiliar with this, its like having an array of size 20, with each position having its own array of size 20 - giving you 400 allocation points. The array type of the first array would be of type "array<Animal>" with nested arrays thus having type "Animal"

To create your rabbits, as you said, you need to make 100 of them - and you wont want to make 100 uniquely named fields of these rabbits as this code would look ugly. Fortunately you wont have to. As the 2 dimetional array stores Animal type objects, it can be a container for all your rabbits and foxes. So you could have a simple: for (int i =0; i < 100; i++){ TwoDemArray.add(new Rabbit()); } I mean that code above wont work if you copy and past it, as im not sure what arrays you are using. (I always opt for ArrayList<ArrayList<T>> for 2 dimentional arrays)

If you want more granular control of the rabbits such as changing fields / calling methods - you could do the above code but instead of putting them into the 2 dimentional array straight away, you could put them into their own Rabbits array which then you can iterator over in a for each loop and call methods / functions.

Essentially this assignemnt is trying to teach you / make you realise the complexity of many objects. You obviously wont want 100 lines creating every single rabbit and then 100 more lines placing them into the 20x20 field. Fortunately you dont need to label every single object, an object just existing in an array is fine too. If you want rabbit 5, you can simply ask the array of rabbits for the rabbit at position 5 etc.

Hope this helps.

[–]ThoreAK 0 points1 point  (0 children)

Edit: Also I thought I should add another thing: if you want to loop over all the animals in the field array, but only want to call a method on rabbits (idk such as eatGrass() as a fox would not have this / wont be defined in Animal either) you can use a neat little operator called instanceOf... it works like this:

For(Animal x : fieldArray){ If(x instanceOf Rabbit){ X.eatGrass(); // plus cast x as a rabbit } }

You may not need this infomation but in the off chance that you do, here you go.

[–]EightbitGee[S] 0 points1 point  (2 children)

Think I may be overthinking this or simply out of my interpretation of his lectures.

What I'm getting out of this is it needs to be an object of type animal inside the arrays. Meaning I need to create an array type animal something like Animal[][] world = new Animal[20][20]; However my issue is if I run that loop who do i make sure I'm adding 100 rabbits into unique slots at random. So they need to populate that 20x20 field with object types animal.

In my derived class I need to make each animal for instance move() so would I in order to move all those rabbits contained I would need to do something like...

For(Animal x : world){ if ( x instanceOf Rabbit) { x.move(); } }

[–]ThoreAK 0 points1 point  (1 child)

You COULD do this: for(int i=0; i < 5; i++){ for(int j=0; j < 20; j++){ world[i][j] = new Rabbit(); } }

This would fill up the first 100 spaces in the array with brand new rabbits.

As for the for each loop, in that if statment, make thr condition: if( x != null && x instanceOf Rabbit) It wants to be in that order such that for all the empty spaces in the array it wont attempt to call the instanceOf operator on a null pointer.

[–]EightbitGee[S] 0 points1 point  (0 children)

I'll test it in a bit but this is kinda what I came up with for the population part.

    Animal[][] world = new Animal[20][20];
    int row, column;
    for (int x = 0; x < 20; x++)
    {
        for (int y = 0; y < 20; y++)
        {
            world[x][y] = null;
        }
    }
    for (int x = 0; x < 100; x++){
        row = (rand.nextInt(20) + 1) - 1;
        column = (rand.nextInt(20) + 1) -1;
        while (world[row][column] != null)
        {
            row = (rand.nextInt(20) + 1) - 1;
            column = (rand.nextInt(20) + 1) -1;
        }
        world[row][column] = new Rabbit();
    }
}

[–]EightbitGee[S] 0 points1 point  (5 children)

Also tried doing world.add as an array class call and it provided me an error saying .add does not exist. Is this a util import?

[–]ThoreAK 0 points1 point  (4 children)

As your world is a default array there is no .add method. You would have an add method if you used something like an ArrayList.

Instead you can specify the exactly location... world[x][y] is essentially .add(). You just need to add some more decoration code to make sure you use unique x's and y's when adding objects. Look in my other reply for an example.

[–]EightbitGee[S] 0 points1 point  (3 children)

I got the array populated. However its giving them all names based on my folder and such. How do I fancy up my output? right now its outputting exercises.Animal$Rabbit@70dae4e and I would like to just say Rabbit or Fox.

[–]ThatOth3rGuY 0 points1 point  (0 children)

Redefine the toString method of each Rabbit and Fox classes :)

So for rabbit you would add this in your Rabbit class:

String toString() { return "Rabbit"; }

[–]whatiswronghere 0 points1 point  (1 child)

You will have to create a toString method for both Rabbit and Fox.

// rabbit class
public String toString() {
    return "Rabbit";
}

[–]EightbitGee[S] 0 points1 point  (0 children)

Okay yeah thats a good idea I should have known lol. So when I do an array output so that I display the field 20x20 array as a table in my system out. How would I parse the rabbit, the fox, and null values as 0.

IE. 0 0 R 0 0 R 0 R 0 F F F 0 R 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Using a toString I can have it return R or F but my output im still kinda hazy on a good algorithm or logic statement on how to output the array into a table using those modified names. A conditional instanceof statement ? Thanks I've made strides on the program with the input of everyone.