0

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

1
  • 1
    public boolean canEat() { needs to be past something before you can make a check, something like public boolean canEat(String food) { Commented Dec 9, 2015 at 0:17

5 Answers 5

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

Comments

0

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

Comments

0

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);
     }
}

Comments

0

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"); 

5 Comments

A bit redundant don't you think? :)
I know but, I wasn't sure if he is allowed to use List or not. That's why I showed him how to convert array to List.
Not about the list part, but about the return :D Look again :p
@Raf No, they mean you could just do return eatsList.contains(food) the if-else statement is redundant
Now I see what you are talking about. I am getting there slowly. Thanks for pointing out though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.