letsdoitjava

Zoo

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