letsdoitjava

Zoo

Jul 14th, 2019
635
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.92 KB | None | 0 0
  1. // Animal Class returns weight of animal
  2. // Renee Waggoner
  3. // July 17, 2019
  4. // Special Requirements: None
  5. package zoo;
  6.  
  7. // Note: If you have a mixture of unique and generalized methods that subclasses will inherit, then use Abstract Class as the super class.
  8. public abstract class Animal
  9. {
  10.     private String name;
  11.     public double weight;
  12.     private int age;
  13.  
  14.     // will be called and used by other classes using key words
  15.     Animal() {}
  16.  
  17.     Animal(String n, double weight, int age)
  18.     {
  19.         name = n;
  20.         this.weight = weight;
  21.         this.age = age;
  22.     }
  23.  
  24.     public abstract String makeNoise();
  25.     public double getWeight()
  26.     {
  27.         return weight;
  28.     }
  29.     // returns a string with info about that animal
  30.     public String toString()
  31.     {
  32.         String str = "Name: " + name + " Weight: " + weight + " age: " + age;
  33.         return str;
  34.     }
  35. }
  36. =======================================================================================================================================
  37. // Cow Class extends Animal class and returns Mooo
  38. // Renee Waggoner
  39. // July 17, 2019
  40. // Special Requirements: None
  41. package zoo;
  42. import zoo.Animal;
  43.  
  44. public class Cow extends Animal
  45. {
  46.     private int num_spots;
  47.  
  48.     Cow(){}
  49.  
  50.     public Cow(String n, double weight, int age, int no_spots)
  51.     {
  52.         super(n, weight, age);
  53.     }
  54.  
  55.     public String makeNoise()
  56.     {
  57.         return "Mooo";
  58.     }
  59.  
  60.     public String toString()
  61.     {
  62.         String str = super.toString() + " Spots: " + num_spots + " Noise: " + makeNoise();
  63.         return str;
  64.     }
  65. }
  66. =======================================================================================================================================
  67. // Horse extends Animal class returns Whinny when makeNoise method is called
  68. // Renee Waggoner
  69. // July 17, 2019
  70. // Special Requirements: None
  71. package zoo;
  72. import zoo.Animal;
  73.  
  74. public class Horse extends Animal
  75. {
  76.     // instance variable
  77.     private double top_speed;
  78.  
  79.     // will be called and used by other classes using key words
  80.     Horse(){}
  81.  
  82.     public Horse(String n, double weight, int age, double top_speed)
  83.     {
  84.         super(n, weight, age);
  85.         this.top_speed = top_speed;
  86.     }
  87.  
  88.     public String makeNoise()
  89.     {
  90.         return "Whinny";
  91.     }
  92.  
  93.     public String toString()
  94.     {
  95.         String str = super.toString() + " speed: " + top_speed + " Noise: " + makeNoise();
  96.         return str;
  97.     }
  98. }
  99. =======================================================================================================================================
  100. // Snake class extends Animal class and returns Hissssss when the makeNoise method is called
  101. // Renee Waggoner
  102. // July 17, 2019
  103. // Special Requirements: None
  104. package zoo;
  105. import zoo.Animal;
  106.  
  107. public class Snake extends Animal
  108. {
  109.     // instance variable
  110.     private int num_fangs;
  111.  
  112.     // will be called and used by other classes using key words
  113.     Snake(){}
  114.  
  115.     public Snake(String n, double weight, int age, int num_fangs)
  116.     {
  117.         super(n, weight, age);
  118.         this.num_fangs = num_fangs;
  119.     }
  120.  
  121.     public String makeNoise()
  122.     {
  123.         return "Hissssss";
  124.     }
  125.  
  126.     public String toString()
  127.     {
  128.         // calling makeNoise method and adding it to this string
  129.         String str = super.toString() + " fangs: " + num_fangs + " Noise: " + makeNoise();
  130.         return str;
  131.     }
  132. }
  133. =======================================================================================================================================
  134. // Zoo Class provides totals of animals and defining details for the animals as long a number of animals is < number of cages
  135. // Renee Waggoner
  136. // July 17, 2019
  137. // Special Requirements: None
  138. package zoo;
  139. import zoo.Animal;
  140. import zoo.Cow;
  141. import zoo.Horse;
  142. import zoo.Snake;
  143.  
  144. public class Zoo {
  145.     private int actual_num_animals;
  146.     private int num_cages;
  147.     private Animal[] animals;
  148.  
  149.     public Zoo()
  150.     {
  151.         actual_num_animals = 0;
  152.         num_cages = 3;
  153.         animals = new Animal[num_cages];
  154.     }
  155.     public Zoo(int num_cages)
  156.     {
  157.         actual_num_animals = 0;
  158.         this.num_cages = num_cages;
  159.         animals = new Animal[num_cages];
  160.     }
  161.  
  162.     public void add(Animal a)
  163.     {
  164.         // stops program from never ending
  165.         if (actual_num_animals < num_cages)
  166.         {
  167.             animals[actual_num_animals] = a;
  168.             actual_num_animals++;
  169.         } else {
  170.             System.out.println("Cannot add more animals to the zoo.");
  171.         }
  172.     }
  173.     public double total_weight()
  174.     {
  175.         // setting decimal because given code had one decimal
  176.         double totalWeight = 0.0;
  177.         // generating the numbers
  178.         for (int i = 0; i < actual_num_animals; i++)
  179.         {
  180.             // calling getWeight method
  181.             totalWeight += animals[i].getWeight();
  182.         }
  183.         return totalWeight;
  184.     }
  185.     public void make_all_noises()
  186.     {
  187.         // generating the numbers
  188.         for (int i = 0; i < actual_num_animals; i++)
  189.         {
  190.             System.out.println(animals[i].makeNoise());
  191.         }
  192.     }
  193.     public void print_all_animals()
  194.     {
  195.         // prints under Animal Printout
  196.         for (int i = 0; i < actual_num_animals; i++)
  197.         {
  198.             System.out.println(animals[i]);
  199.         }
  200.     }
  201.  
  202.  
  203.     public static void main(String[] args)
  204.     {  
  205.         Zoo z = new Zoo(4);
  206.         Snake sly = new Snake("Sly", 5.0 , 2, 2);
  207.         Snake sly2 = new Snake("Slyme", 10.0 , 1, 2);
  208.         Cow blossy = new Cow("Blossy", 900., 5, 10);
  209.         Horse prince = new Horse("Prince", 1000., 5, 23.2);
  210.  
  211.         //Following not allowed because Animal is abstract
  212.         //Animal spot = new Animal("Spot", 10., 4);
  213.  
  214.         z.add(sly);
  215.         z.add(sly2);
  216.         z.add(blossy);
  217.         z.add(prince);
  218.  
  219.         z.make_all_noises();
  220.         // calling total_weight method to print total
  221.         System.out.println("Total weight = " + z.total_weight());
  222.         System.out.println("**************************");
  223.         System.out.println("Animal Printout:");
  224.         z.print_all_animals();
  225.  
  226.         System.out.println("********* Now we will make the Second Zoo");
  227.         Zoo z2 = new Zoo(5);
  228.         z2.add(sly);
  229.         z2.add(sly2);
  230.         z2.add(blossy);
  231.         z2.add(prince);
  232.         z2.add( new Horse("Warrior", 1200, 6, 25.3));
  233.         z2.add( new Horse("Harry", 1100, 4, 21.3));
  234.         // printing total weight using total_weight method
  235.         System.out.println("Total weight of z2 = "+z2.total_weight());
  236.         z2.make_all_noises();
  237.         z2.print_all_animals();
  238.     }// end of main
  239. }// end of zoo class
Advertisement
Add Comment
Please, Sign In to add comment