You are here

The Food Consumers

26 July, 2019 - 09:51
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6

Vegetarians and carnivores are basically the same animals. They have the basic ingredients such as salt and pepper to cook food. They differ in the kind of raw materials they stock to cook their foods and in the way they order food from a chef. Vegetarians and Carnivores can provide the materials to cook but do not know how to cook! In order to get any cooked meal, they have to ask a chef to cook for them. We model them as two concrete subclasses of an abstract class called AEater. AEater has two concrete methods, getSalt and getPepper, and an abstract method called order, as shown in the table below.

Table 3.6 Top-level abstract denition
public abstract class AEater {
public String getSalt() {
       return "salt";
}
public String getPepper() {
return "pepper";
}
/**
* Orders n portions of appropriate food from restaurant r.
*/
public abstract String order(IChef r, Integer n);
// NO CODE BODY!
}
 
Table 3.7 Concrete implementations
public class Vegetarian extends AEater{
public String getBroccoli() {  
        return "broccoli";
}
public String getCorn() {
return "corn";
}
public String order(IChef c, Object n) {
// code to be discussed later;
}
}

 
public class Carnivore extends AEater{
public String getMeat() {
return "steak";
}
public String getChicken() { return "cornish hen"; }
public String getDog() {
return "polish sausage";
}
public String order(IChef c, Object n) {
// code to be discussed later;
}
}