letsdoitjava

Zoo

Jul 15th, 2019
354
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 has 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.  
  139. // Zoo Class provides totals of animals and defining details for the animals as long a number of animals is < number of cages
  140. // Renee Waggoner
  141. // July 22, 2019
  142. // Special Requirements: None
  143. package zoo;
  144.  
  145. public class Zoo {
  146.     private int actual_num_animals;
  147.     private int num_cages;
  148.     private Animal[] animals;
  149.    
  150.     Zoo()
  151.     {
  152.         num_cages = 3;
  153.     }
  154.  
  155.     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.     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.         }// end else statement
  172.     }
  173.    
  174.     double total_weight()
  175.     {
  176.         // setting decimal because given code had one decimal
  177.         double totalWeight = 0.0;
  178.         // generating the numbers
  179.         for (int i = 0; i < actual_num_animals; i++)
  180.         {
  181.             // calling getWeight method
  182.             totalWeight += animals[i].getWeight();
  183.         }
  184.         return totalWeight;
  185.     }
  186.    
  187.     void make_all_noises()
  188.     {
  189.         // generating the numbers
  190.         for (int i = 0; i < actual_num_animals; i++)
  191.         {
  192.             System.out.println(animals[i].makeNoise());
  193.         }
  194.     }
  195.    
  196.     void print_all_animals()
  197.     {
  198.         // prints under Animal Printout
  199.         for (int i = 0; i < actual_num_animals; i++)
  200.         {
  201.             System.out.println(animals[i]);
  202.         }
  203.     }
  204.  
  205.     public static void main(String[] args)
  206.     {  
  207.         Zoo z = new Zoo(4);
  208.         Snake sly = new Snake("Sly", 5.0 , 2, 2);
  209.         Snake sly2 = new Snake("Slyme", 10.0 , 1, 2);
  210.         Cow blossy = new Cow("Blossy", 900., 5, 10);
  211.         Horse prince = new Horse("Prince", 1000., 5, 23.2);
  212.  
  213.         //Following not allowed because Animal is abstract
  214.         //Animal spot = new Animal("Spot", 10., 4);
  215.  
  216.         z.add(sly);
  217.         z.add(sly2);
  218.         z.add(blossy);
  219.         z.add(prince);
  220.  
  221.         z.make_all_noises();
  222.         // calling total_weight method to print total
  223.         System.out.println("Total weight = " + z.total_weight());
  224.         System.out.println("**************************");
  225.         System.out.println("Animal Printout: ");
  226.         z.print_all_animals();
  227.  
  228.         System.out.println("********* Now we will make the Second Zoo");
  229.         Zoo z2 = new Zoo(5);
  230.         z2.add(sly);
  231.         z2.add(sly2);
  232.         z2.add(blossy);
  233.         z2.add(prince);
  234.         z2.add( new Horse("Warrior", 1200, 6, 25.3));
  235.         z2.add( new Horse("Harry", 1100, 4, 21.3));
  236.         // printing total weight using total_weight method for second zoo
  237.         System.out.println("Total weight of z2 = "+z2.total_weight());
  238.         z2.make_all_noises();
  239.         z2.print_all_animals();
  240.     }// end of main
  241. }// end of zoo class
Advertisement
Add Comment
Please, Sign In to add comment