mardi 8 décembre 2015

How do I do an if else statement that loops over an array of strings?

I am basically writing a virtual zoo. I have an animal class which contains an array called eats which contains strings of foods. I have other classes, such as lion and tiger which extend my animal class. Within these classes I have specified what food they eat as this is unique for each animal. For example, a lion eats steak and celery. I have now been asked to create a canEat() method in my animal class which basically returns true if that particular animal can eat that food. How would I go about doing this?

Here is my animal class

public abstract class Animal {
    String[] eats = new String[] {"steak", "celery", "fish", "fruit"};

    public boolean canEat() {

    }
}

and here is how I specify what each specific animal can eat in their respected class

public class Lion extends Animal {
     public Lion (String[] eats) {
          super(new String[] {"steak", "celery"});
     }
}

Thanks in advance, I hope I explained this clearly

Aucun commentaire:

Enregistrer un commentaire