Reputation: 45
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
Upvotes: 0
Views: 59
Reputation: 17132
Define the canEat
method as follows:
public boolean canEat(String food) {
return Arrays.asList(eats).contains(food);
}
Here is how you can use it:
public static void main(String args[]) {
Lion lion = new Lion(new String[] {"steak", "meat", "otherFood"});
System.out.println("Can the lion eat steak ? " + lion.canEat("steak"));
// System.out: Can the lion eat steak ? true
System.out.println("Can the lion eat meat loaf ? " + lion.canEat("meat loaf"));
// System.out: Can the lion eat meat loaf ? false
}
Upvotes: 2
Reputation: 7649
You could convert your array to List of Strings as List<String>
and then check if list contains
the food (passed as argument to canEat). Modify your canEat to as follow
public boolean canEat(String food) {
List<String> eatsList = Arrays.asList(eats);
//if(eatsList.contains(food)) {
// return true;
//} else {
// return false;
//}
//as pointed out in comments would do exactly as above four lines of code
return eatsList.contains(food);
}
Then when you create object you invoke canEat()
and pass it the food as argument.
lion.canEat("fish");
Upvotes: 0
Reputation: 19956
So full answer can be
public abstract class Animal {
private String[] eats;
public Animal(String[] eats) {
this.eats = eats;
}
public boolean canEat(String food) {
return Arrays.asList(eats).contains(food);
}
}
public class Lion extends Animal {
public Lion (String[] eats) {
super(eats);
}
}
Upvotes: 0
Reputation: 347194
You'll need to change one of your classes slightly, for example, you could do something like...
public abstract class Animal {
String[] eats;
public Animal(String[] canEat) {
eats = canEat;
}
public boolean canEat(String food) {
boolean canEat = false;
for (String eatiable :eats) {
if (eatiable.equals(food)) {
canEat = true;
break;
}
}
return canEat;
}
}
Equally, you could make canEat
abstract
and make the child classes deal with it.
This does a case-sensitive comparison, if you need to a case-insensitive comparison, you could use eatiable.equalsIgnoresCase(food)
instead
Upvotes: 0
Reputation: 386
Many ways to solve this, using a for loop:
public boolean canEat(String food) {
for (int i = 0; i < eats.length; i++) {
if (eats[i].equals(food))
return true;
}
return false;
}
Upvotes: 0