idastan97

CSCI152 L5 P1

Feb 1st, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////// Animal Interface
  2. public interface Animal {
  3.    
  4.     /**
  5.      * Returns the kind of the animal.
  6.      *
  7.      * @return the kind of the animal
  8.      */
  9.     public String animalKinde();
  10.    
  11.     /**
  12.      * returns the current age of the animal.
  13.      *
  14.      * @return the age of the animal
  15.      */
  16.     public int getAge();
  17.    
  18.     /**
  19.      * Returns true if the animal can eat the given food.
  20.      *
  21.      * @param food - the name of food
  22.      * @return whether or not the animal can the given food
  23.      */
  24.     public boolean canEat(String food);
  25.    
  26.     /**
  27.      *
  28.      * @param food
  29.      * @throws Exception
  30.      */
  31.     public void feed(String food) throws Exception;
  32.    
  33.     /**
  34.      * The animal sleeps
  35.      */
  36.     public void sleep();
  37.    
  38.     /**
  39.      * Increments the animal's age by one year.
  40.      * @throws java.lang.Exception
  41.      */
  42.     public void ageOneYear() throws Exception;
  43. }
  44.  
  45. //////////////////////////////////////////////////////////////////// Animal Implementation
  46. public class AnimalImpl implements Animal{
  47.    
  48.     private int age;
  49.     private int maxAge;
  50.     private boolean isAlive;
  51.    
  52.     public AnimalImpl(int ma){
  53.         age=0;
  54.         maxAge=ma;
  55.         isAlive=true;
  56.     }
  57.    
  58.     @Override
  59.     public String animalKinde() {
  60.         return "undefined.";
  61.     }
  62.  
  63.     @Override
  64.     public int getAge() {
  65.         return age;
  66.     }
  67.  
  68.     @Override
  69.     public boolean canEat(String food) {
  70.         return false;
  71.     }
  72.    
  73.     @Override
  74.     public void feed(String food) throws Exception{
  75.         if (!this.canEat(food)){
  76.             throw new Exception("The animal cannot eat "+food);
  77.         }
  78.         System.out.println("Nom Nom");
  79.     }
  80.    
  81.     @Override
  82.     public void sleep()
  83.     {
  84.         System.out.println("Z-z-z");
  85.     }
  86.    
  87.     @Override
  88.     public void ageOneYear() throws Exception{
  89.         if (age>=maxAge){
  90.             throw new Exception("The animal was died.");
  91.         }
  92.         age++;
  93.     }
  94.    
  95.     public String toString(){
  96.         return "Animal which is "+age+" years old.";
  97.     }
  98. }
  99.  
  100. /////////////////////////////////////////////////////////////////////////////// Tiger
  101. public class Tiger extends AnimalImpl{
  102.    
  103.     public Tiger(){
  104.         super(12);
  105.     }
  106.    
  107.     public String animalKinde(){
  108.         return "Tiger";
  109.     }
  110.    
  111.     public boolean canEat(String food){
  112.         if (food.equals("Meat")){
  113.             return true;
  114.         }
  115.         return false;
  116.     }
  117.    
  118.     public void feed(String food) throws Exception{
  119.         if (!this.canEat(food)){
  120.             throw new Exception("Tigers cannot eat "+food);
  121.         }
  122.         System.out.println("Munch Munch!");
  123.     }
  124.    
  125.     public String toString(){
  126.         return "Tiger which is " + getAge() + " years old.";
  127.     }
  128. }
  129.  
  130. //////////////////////////////////////////////////////////////////////////// Cow
  131. public class Cow extends AnimalImpl{
  132.    
  133.     public Cow(){
  134.         super(10);
  135.     }
  136.    
  137.     public String animalKinde(){
  138.         return "Cow";
  139.     }
  140.    
  141.     public boolean canEat(String food){
  142.         if (food.equals("Grass")){
  143.             return true;
  144.         }
  145.         return false;
  146.     }
  147.    
  148.     public void feed(String food) throws Exception{
  149.         if (!this.canEat(food)){
  150.             throw new Exception("Cows cannot eat "+food);
  151.         }
  152.         System.out.println("nooom nooom nooom");
  153.     }
  154.    
  155.     public String toString(){
  156.         return "Cow which is " + getAge() + " years old.";
  157.     }
  158. }
  159.  
  160. ////////////////////////////////////////////////////////////////////////////////// Monkey
  161. public class Monkey extends AnimalImpl{
  162.    
  163.     public Monkey(){
  164.         super(15);
  165.     }
  166.    
  167.     public String animalKinde(){
  168.         return "Monkey";
  169.     }
  170.    
  171.     public boolean canEat(String food){
  172.         if (food.equals("Banana")){
  173.             return true;
  174.         }
  175.         return false;
  176.     }
  177.    
  178.     public void feed(String food) throws Exception{
  179.         if (!this.canEat(food)){
  180.             throw new Exception("Monkeys cannot eat "+food);
  181.         }
  182.         System.out.println("nom");
  183.     }
  184.    
  185.     public String toString(){
  186.         return "Monkey which is " + getAge() + " years old.";
  187.     }
  188. }
  189.  
  190. /////////////////////////////////////////////////////////////////////// Test Class
  191. public class TestAnimal {
  192.    
  193.     public static void feedMeat(Animal animal){
  194.         try{
  195.             animal.feed("Meat");
  196.         } catch (Exception ex){
  197.             System.out.println(ex.getMessage());
  198.         }
  199.     }
  200.     public static void main(String[] args){
  201.         Animal cow=new Cow();
  202.         Animal tiger=new Tiger();
  203.         Animal monkey=new Monkey();
  204.         for (int i=0; i<11; i++){
  205.             try {
  206.                 cow.ageOneYear();
  207.                 System.out.println(cow);
  208.             } catch (Exception ex){
  209.                 System.out.println(ex.getMessage());
  210.             }
  211.         }
  212.        
  213.         try{
  214.             feedMeat(tiger);
  215.         } catch (Exception ex){
  216.             System.out.println(ex.getMessage());
  217.         }
  218.        
  219.         try{
  220.             feedMeat(monkey);
  221.         } catch (Exception ex){
  222.             System.out.println(ex.getMessage());
  223.         }
  224.        
  225.     }
  226.    
  227. }
Advertisement
Add Comment
Please, Sign In to add comment