Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. public interface Animal
  2. {
  3.   public String getSound();
  4.   public String getType();
  5. }
  6.  
  7. import java.util.*;
  8. public class Chick implements Animal
  9. {
  10.     private String myType;
  11.     private String mySound1;
  12.     private String mySound2;
  13.     private boolean status;
  14.  
  15.  
  16.   Chick()
  17.   {
  18.     myType = "chick";
  19.     mySound1 = "cheep";
  20.     mySound2 = "cluck";
  21.     status=true;
  22.  
  23.   }
  24.  
  25.   Chick(boolean mood)
  26.   {
  27.     myType= "chick";
  28.     mySound2 = "cluck";
  29.     mySound1 = "cheep";
  30.     status=mood;
  31.   }
  32.  
  33.   public String getSound()
  34.   {
  35.     Random random = new Random();
  36.     int t = random.nextInt(2);
  37.     if(status==true)
  38.         if(t!=0)
  39.             return mySound1;
  40.         return mySound2;
  41.   }
  42.  
  43.   public String getType()
  44.   {
  45.     return myType;
  46.   }
  47. }
  48.  
  49. public class Pig implements Animal
  50. {
  51.   private String myType;
  52.   private String mySound;
  53.  
  54.   Pig()
  55.   {
  56.     myType = "pig";
  57.     mySound = "oink";
  58.   }
  59.  
  60.   public String getSound()
  61.   {
  62.     return mySound;
  63.   }
  64.  
  65.   public String getType()
  66.   {
  67.     return myType;
  68.   }
  69. }
  70.  
  71. public class Cow implements Animal
  72. {
  73.   private String myType;
  74.   private String mySound;
  75.  
  76.   Cow()
  77.   {
  78.     myType = "cow";
  79.     mySound = "moo";
  80.   }
  81.  
  82.   public String getSound()
  83.   {
  84.     return mySound;
  85.   }
  86.  
  87.   public String getType()
  88.   {
  89.     return myType;
  90.   }
  91. }
  92.  
  93. import java.util.*;
  94. public class NamedCow extends Cow
  95. {
  96.     private String myName;
  97.  
  98.  
  99.     NamedCow(String name)
  100.     {
  101.         super();
  102.         myName=name;
  103.  
  104.     }
  105.  
  106.     public String getName()
  107.     {
  108.         return myName;
  109.     }
  110. }
  111.  
  112. import java.util.*;
  113. public class Farm
  114. {
  115.    private ArrayList <Animal> myFarm;
  116.  
  117.    public Farm()
  118.    {
  119.       myFarm = new ArrayList <Animal>();
  120.       myFarm.add(new Cow());
  121.       myFarm.add(new Chick(true));
  122.       myFarm.add(new Pig());
  123.       myFarm.add(new NamedCow("Elsie"));
  124.    }
  125.  
  126.    public void animalSounds()
  127.    {
  128.       Animal temp;
  129.       for(int i = 0; i < myFarm.size(); i++)
  130.       {
  131.          temp = myFarm.get(i);
  132.          System.out.println(temp.getType() + " goes " + temp.getSound());
  133.       }
  134.  
  135.       NamedCow named = (NamedCow)myFarm.get(4);
  136.       System.out.println(named.getName());
  137.    }
  138. }
  139.  
  140. public class OldMacDonald
  141. {
  142.   public static void main(String[] args)
  143.   {
  144.     Farm farm = new Farm();
  145.     farm.animalSounds();
  146.  
  147.  
  148.  
  149.   }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement