-1

For my assignment, I am trying to throw an exception so that my program does not allow objects "Wolf" to eat "Plants". I am however struggling to find a way to implement this. I have so far tried using an if statement to search for the condition of food (x) being equal to "Plants" but this does not seem to be working. Here is the code:

Animal class

abstract public class Animal 
{

String name;
int age;  
String noise;

abstract public void makeNoise();

public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName;
    }

abstract public Food eat(Food x) throws Exception;

}

Food class

public class Food {

    //field that stores the name of the food
    public String name; 

    //constructor that takes the name of the food as an argument
    public Food(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Carnivore class

public class Carnivore extends Animal
{//if statement that throws exception
   public Food eat(Food x) throws Exception
    { 
        if (x.equals(new Meat("Plants"))) {
                throw new Exception("Carnivores only eat meat!");
            } else {
                return x;
            }

    }
    public void makeNoise()  
    {
        noise = null;
    }
    public String getNoise()  
    {
        return noise;
    }   
}

Meat class

public class Meat extends Food 
{

    public Meat(String name) {
        super(name);
    }

    public String getName() {
    return super.getName();
}
}

Wolf class

public class Wolf extends Carnivore
{

Wolf()   
{
    name = "Alex";
    age = 4;

}
    public void makeNoise()  
    {
        noise = "Woof!";
    }
    public String getNoise()  
    {
        return noise;
    }
    public String getName() 
    {
        return name;
    }  
    public int getAge()
    {
        return age;
    }

    public String eat(String x)
    {
        return x;
    }

}

Main

public class Main {

    public static void main(String[] args) 
    {

        Wolf wolfExample = new Wolf();        
        System.out.println("************Wolf\"************");
        System.out.println("Name = " + wolfExample.getName());
        System.out.println("Age = " + wolfExample.getAge());
        wolfExample.makeNoise();
        System.out.println("Noise = " + wolfExample.getNoise());

        Meat meatExample = new Meat("Plants");
        System.out.println("************Wolf eating habits************");
        System.out.println("Wolves eat " + wolfExample.eat(meatExample.getName()));
    }        
}

Output

************Wolf"************
Name = Alex
Age = 4
Noise = Woof!
************Wolf eating habits************
Wolves eat Plants//this should throw exception message

Any help on how to fix this to get the desired output would be greatly appreciated, thanks.

Harry
  • 109
  • 1
  • 8
  • 1
    The `new Meat("Plants")` part seems quite absurd. Have you tried establishing a hierarchy of `Food` items so that `Carnivore`s can only `eat(CarnivoreFood food)`? – Mena Nov 23 '16 at 14:53
  • 2
    Either don't override `eat` in your `Wolf` class, or call `super.eat(x)` . – Arnaud Nov 23 '16 at 14:53

1 Answers1

0

This code is causing the problem:

if (x.equals(new Meat("Plants"))) {
                throw new Exception("Carnivores only eat meat!");
            } else {
                return x;
            }
  1. You don't have a equals method defined in your classes, so it compares objects using == operator - checking if the references are the same.
  2. This expression:

    x == (new Meat("Plants"))

    is always false - new operator creates new instance of Meat object so the reference is always different.

  3. Do not use equals to check types, use instanceof operator instead.

So your code should look like this:

 public Food eat(Food x) throws Exception
    { 
        if (x instanceof Meat) {
                return x;
            } else {
               throw new Exception("Carnivores only eat meat!");
            }
    }

In that case you will need to define Plant class that extends Food.

Alternatively you can define equals method in your Food class that compares name field. How to override equals method in java

Community
  • 1
  • 1
Dominik Kunicki
  • 1,037
  • 1
  • 12
  • 35