Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Animal Class returns name, age, and weight of animal
- // Renee Waggoner
- // July 22, 2019
- // Special Requirements: None
- package zoo;
- // Note: If you have a mixture of unique and generalized methods that subclasses will inherit, then use Abstract Class as the super class.
- // abstract doesn't allow user to modify the class
- public abstract class Animal
- {
- private String name;
- public double weight;
- private int age;
- // default method
- Animal() {}
- // will be called and used by other classes using key words
- Animal(String n, double weight, int age)
- {
- name = n;
- this.weight = weight;
- this.age = age;
- }
- // abstract so that user can't change it
- abstract String makeNoise();
- double getWeight()
- {
- return weight;
- }
- // returns a string with info about that animal
- public String toString()
- {
- String str = "Name: " + name + " Weight: " + weight + " age: " + age;
- return str;
- }
- }
- =======================================================================================================================================
- // Cow Class extends Animal class and returns Mooo
- // Renee Waggoner
- // July 22, 2019
- // Special Requirements: None
- package zoo;
- public class Cow extends Animal
- {
- private int num_spots;
- // default method
- Cow(){}
- Cow(String n, double weight, int age, int no_spots)
- {
- super(n, weight, age);
- }
- String makeNoise()
- {
- return "Mooo";
- }
- public String toString()
- {
- String str = super.toString() + " Spots: " + num_spots + " Noise: " + makeNoise();
- return str;
- }
- }
- =======================================================================================================================================
- // Horse extends Animal class returns Whinny when makeNoise method is called
- // Renee Waggoner
- // July 22, 2019
- // Special Requirements: None
- package zoo;
- public class Horse extends Animal
- {
- // instance variable
- private double top_speed;
- // will be called and used by other classes using key words
- Horse(){}
- Horse(String n, double weight, int age, double top_speed)
- {
- super(n, weight, age);
- this.top_speed = top_speed;
- }
- String makeNoise()
- {
- return "Whinny";
- }
- public String toString()
- {
- String str = super.toString() + " speed: " + top_speed + " Noise: " + makeNoise();
- return str;
- }
- }
- =======================================================================================================================================
- // Snake class extends Animal class and returns Hissssss when the makeNoise method is called
- // Renee Waggoner
- // July 22, 2019
- // Special Requirements: None
- package zoo;
- public class Snake extends Animal
- {
- // instance variable
- private int num_fangs;
- // will be called and used by other classes using key words
- // Note: constructor have to have the same name as the class
- // this is a default constructor
- Snake(){}
- public Snake(String n, double weight, int age, int num_fangs)
- {
- // calling animal constructor
- super(n, weight, age);
- this.num_fangs = num_fangs;
- }
- public String makeNoise()
- {
- return "Hissssss";
- }
- public String toString()
- {
- // calling makeNoise method and adding it to this string
- String str = super.toString() + " fangs: " + num_fangs + " Noise: " + makeNoise();
- return str;
- }
- }
- =======================================================================================================================================
- // Zoo Class provides totals of animals and defining details for the animals as long a number of animals is < number of cages
- // Renee Waggoner
- // July 22, 2019
- // Special Requirements: None
- package zoo;
- public class Zoo {
- private int actual_num_animals;
- private int num_cages;
- private Animal[] animals;
- Zoo()
- {
- num_cages = 3;
- }
- Zoo(int num_cages)
- {
- actual_num_animals = 0;
- this.num_cages = num_cages;
- animals = new Animal[num_cages];
- }
- void add(Animal a)
- {
- // stops program from never ending
- if (actual_num_animals < num_cages)
- {
- animals[actual_num_animals] = a;
- actual_num_animals++;
- } else {
- System.out.println("Cannot add more animals to the zoo.");
- }// end else statement
- }
- double total_weight()
- {
- // setting decimal because given code had one decimal
- double totalWeight = 0.0;
- // generating the numbers
- for (int i = 0; i < actual_num_animals; i++)
- {
- // calling getWeight method
- totalWeight += animals[i].getWeight();
- }
- return totalWeight;
- }
- void make_all_noises()
- {
- // generating the numbers
- for (int i = 0; i < actual_num_animals; i++)
- {
- System.out.println(animals[i].makeNoise());
- }
- }
- void print_all_animals()
- {
- // prints under Animal Printout
- for (int i = 0; i < actual_num_animals; i++)
- {
- System.out.println(animals[i]);
- }
- }
- public static void main(String[] args)
- {
- Zoo z = new Zoo(4);
- Snake sly = new Snake("Sly", 5.0 , 2, 2);
- Snake sly2 = new Snake("Slyme", 10.0 , 1, 2);
- Cow blossy = new Cow("Blossy", 900., 5, 10);
- Horse prince = new Horse("Prince", 1000., 5, 23.2);
- //Following not allowed because Animal is abstract
- //Animal spot = new Animal("Spot", 10., 4);
- z.add(sly);
- z.add(sly2);
- z.add(blossy);
- z.add(prince);
- z.make_all_noises();
- // calling total_weight method to print total
- System.out.println("Total weight = " + z.total_weight());
- System.out.println("**************************");
- System.out.println("Animal Printout: ");
- z.print_all_animals();
- System.out.println("********* Now we will make the Second Zoo");
- Zoo z2 = new Zoo(5);
- z2.add(sly);
- z2.add(sly2);
- z2.add(blossy);
- z2.add(prince);
- z2.add( new Horse("Warrior", 1200, 6, 25.3));
- z2.add( new Horse("Harry", 1100, 4, 21.3));
- // printing total weight using total_weight method for second zoo
- System.out.println("Total weight of z2 = "+z2.total_weight());
- z2.make_all_noises();
- z2.print_all_animals();
- }// end of main
- }// end of zoo class
Advertisement
Add Comment
Please, Sign In to add comment