I know there are many questions similar to mine already but I still haven't found a solution that applies to my problem. I am required to use three different classes for this and to give you an idea, here is what some of my code looks like (included only those that I think are relevant):
public class Main {
//this is where I instantiate
public static void main(String[] args){
Habitat habitat1 = new Habitat(some arguments/parameters here);
Habitat habitat2 = new Habitat(some arguments/parameters here);
Animal type1 = new Animal(some arguments/parameters here);
Animal type2 = new Animal(some arguments/parameters here);
Animal type3 = new Animal(some arguments/parameters here);
Animal type4 = new Animal(some arguments/parameters here);
}
}
public class Habitat {
//other attributes here
Animal[] animals;
public Habitat(some arguments/parameters here) {
//here I want to be able to reference/assign **Animal type1 and type2 to habitat1** and **Animal type3 and type4 to habitat2**
}
public class Animal {
//other attributes here
Habitat habitat;
public Animal(some arguments/parameters here){
}
I know that in order to have an array of object animals, I need to write something like this:
Animal[] animals = new Animal[4];
animals[0] = type1;
animals[1] = type2;
animals[2] = type3;
animals[3] = type4;
The problem is that I cannot put this block of code in class Habitat because it cannot access type1-type4. When I put this in class Main, I am unable to reference it in class Habitat.
Basically, the specific output I want in this particular problem is that when I print the contents of habitat1 for example, it will look something like this:
Habitat 1 - Savanna
Mixed woodland-grassland
List of Animals:
Type 1
Name: Tiger
Diet: Carnivore
Type 2
Name: Elephant
Diet: Herbivore
Add a method addAnimal( Animal animal )
to Habitat
and call this from the constructor of Animal
like this
…
habitat.addAnimal( this );
…
Of course this assumes that the fourth argument of the Animal
constructor is named habitat
.
And the animals
attribute should not be an array, but an instance of java.util.List
(preferrably a java.util.ArrayList
). Then the new method would look like this:
public final void addAnimal( final Animal animal )
{
animals.add( animal );
}
Perhaps you have to add some checks (not-null, for example), and when you want to make sure that an animal can be added just once, you should consider to use java.util.Set
instead of a List
(But this would mean some changes to the Animal
class as well).
If you really insist on an array for the attribute, the addAnimal()
method would get a bit more complex:
public final void addAnimal( final Animal animal )
{
if( animals == null )
{
animals = new Animals [1];
animals [0] = animal;
}
else
{
var curLen = animals.length;
var newAnimals = new Animal [curLen + 1];
System.arraycopy( animals, 0 newAnimals, 0, curLen )
animals = newAnimals;
animals [curLen] = animal;
}
}
or
public final void addAnimal( final Animal animal )
{
if( animals == null )
{
animals = new Animals [1];
animals [0] = animal;
}
else
{
var curLen = animals.length;
animals = Arrays.copyOf( animals, curLen + 1 );
animals [curLen] = animal;
}
}
Still error handling is missing.
Provide method(s) on Habitat
for associating the Animal[]
.
For instance, if you define a constructor that accepts the Animal[]
then you can build that array first and then pass it into the constructor. You can also provide a setter that allows you to swap to a different Animal[]
at any point.
public class Habitat {
private Animal[] animals; // note that this should be private
public Habitat(Animal[] animals) {
this.animals = animals;
}
public void setAnimals(Animal[] animals) {
this.animals = animals;
}
}
This allows the main method to pre-generate the Animals or assign them later as needed.
public class Main {
public static void main(String[] args) {
Animal[] animals = ... build the array
Habitat habitat1 = new Habitat(animals);
Habitat habitat2 = new Habitat();
Animal[] moreAnimals = ... build another array of Animals
habitat2.setAnimals(moreAnimals);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.