Advertisement
Guest User

Animal Zook

a guest
Dec 8th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. Animal.java
  2. ====================
  3.  
  4. public class Animal {
  5.     //fields
  6.     protected boolean _isAlive; //indicated if animal alive
  7.     protected String _name; //animal name
  8.     protected String _sound; //the sound that the animal makes
  9.    
  10.     public Animal (String name, String sound)
  11.     {
  12.         this._isAlive=true;
  13.         this._name=name;
  14.         this._sound=sound;
  15.     }
  16.    
  17.     public String makeSound()
  18.     {
  19.         if (this._isAlive)
  20.             return this._name + " makes " + this._sound;
  21.         else
  22.             return this._name + " is dead...";
  23.                
  24.     }
  25.    
  26.     public void die()
  27.     {
  28.         this._isAlive=false;
  29.     }
  30. }
  31.  
  32.  
  33. Mouse.java
  34. ==================
  35.  
  36. public class Mouse extends Animal {
  37.  
  38.     public Mouse(String name, String sound) {
  39.         super(name, sound);
  40.     }
  41.  
  42. }
  43.  
  44. Cat.java
  45. ======================
  46.  
  47. public class Cat extends Animal{
  48.  
  49.     public Cat(String name, String sound) {
  50.         super(name, sound);
  51.     }
  52.  
  53.     public void eatMouse(Mouse mouse)
  54.     {
  55.         if (this._isAlive && mouse._isAlive)
  56.             mouse.die();
  57.     }
  58.    
  59.     public void drinkMilk(Cow cow)
  60.     {
  61.         if (cow.giveMilk())
  62.             System.out.println("Yamm Yamm!");
  63.         else
  64.             System.out.println("Yak, "+cow._name+" is dead");
  65.     }
  66. }
  67.  
  68. Cow.java
  69. ====================
  70.  
  71. public class Cow extends Animal {
  72.  
  73.     public Cow(String name, String sound) {
  74.         super(name, sound);
  75.     }
  76.  
  77.     public boolean giveMilk()
  78.     {
  79.         return this._isAlive;
  80.     }
  81. }
  82.  
  83.  
  84. Lion.java
  85. ===================
  86.  
  87. public class Lion extends Animal {
  88.  
  89.     public Lion(String name, String sound) {
  90.         super(name, sound);
  91.     }
  92.    
  93.     public void eatCow(Cow cow)
  94.     {
  95.         if (this._isAlive && cow._isAlive)
  96.         {
  97.             cow.die();
  98.         }
  99.     }
  100. }
  101.  
  102.  
  103. Tester.java
  104. ================
  105.  
  106. public class Tester {
  107.  
  108.     public static void main(String[] args) {
  109.         final String MOUSE_SOUND="SQuitzz";
  110.         final String CAT_SOUND="MEYOUUU";
  111.         final String COW_SOUND="MOOOOOOO";
  112.         final String LION_SOUND="GRRRRRR";
  113.        
  114.         Mouse m1=new Mouse("Mishka",MOUSE_SOUND);
  115.         Mouse m2=new Mouse("Zeev",MOUSE_SOUND);
  116.        
  117.         Cat cat1=new Cat("Mitzi",CAT_SOUND);
  118.         Cat cat2=new Cat("Shushi",CAT_SOUND);
  119.        
  120.         Cow cow1=new Cow("MushMush", COW_SOUND);
  121.         Cow cow2=new Cow("Mumu", COW_SOUND);
  122.        
  123.         Lion lev = new Lion("LEV",LION_SOUND);
  124.        
  125.         System.out.println(lev.makeSound());
  126.         System.out.println(cow1.makeSound());
  127.         System.out.println(cat1.makeSound());
  128.         System.out.println(m1.makeSound());
  129.         System.out.println("==================");
  130.         cat1.eatMouse(m1);
  131.         System.out.println(m1.makeSound());
  132.         cat1.drinkMilk(cow1);
  133.         lev.eatCow(cow1);
  134.         cat1.drinkMilk(cow1);
  135.         cat1.drinkMilk(cow2);
  136.        
  137.                
  138.     }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement