James James - 9 days ago 3
Java Question

How to pass an argument that requires a parameter of type Food?

I am writing a program that is based on the demonstration of inheritance. I am trying to write an exception so that the only parameter that can be passed into the Meat class which is linked to the class Wolf. In essence, I am trying to allow the only parameter that can be passed into the eating method to be a Food variable called Meat. Here is the code:

Animal

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

public class Food {

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

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

public Food getName() {
return name;
}
}


Meat

public class Meat extends Food
{

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

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


Carnivore

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 Food eat(Food x) throws Exception
{
if (x instanceof Meat) {
return x;
} else {
throw new Exception("Carnivores only eat meat!");
}
}
}


Wolf

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 Food eat(Food x) throws Exception
{
if (x instanceof Meat) {
return x;
} else {
throw new Exception("Carnivores only eat meat!");
}
}
}


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(//Food argument goes here?);
System.out.println("************Wolf eating habits************");
System.out.println("Wolves eat " + wolfExample.eat(meatExample.getName()));
}
}


The problem I'm having is that I cannot pass in anything as a food argument within the new Meat object that I create within my main method. And I mm getting the error of an unsupported exception when I try to call
System.out.println("Wolves eat " + wolfExample.eat(meatExample.getName()));
which I think may be because a Food variable has not been passed in. The desired outcome is that a Food variable such as Plants is passed in which throws an exception message. Any help on how to resolve this is appreciated, thanks.

Answer

You will have to modify your Animal and Food classes first and then with few other changes in your Main class, you may be able to achieve what you are trying to. Here are few suggested changes:

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

public class Meat extends Food 
{
    public Meat(String name) {
        super(name);
    }

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


public class Main {
    public Main() {
        super();
    }

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

        try {            
            Meat meatExample = new Meat("Steak");
            //Food vegFood =  new Food("Spinach");
                    System.out.println("************Wolf eating habits************");
            wolfExample.eat(meatExample);
            //wolfExample.eat(vegFood);

        } catch (Exception e) {
            // TODO: Add catch code
            e.printStackTrace();
        }
    }
}

So, If you call wolfExample.eat(vegFood); your code will throw exception.

Comments