Complete with comments the following code: You run a zoo which contains Animals.
ID: 3641750 • Letter: C
Question
Complete with comments the following code:You run a zoo which contains Animals.
So each Animal will be represented
by an abstract class.
Consider creating classes Animal, Cow, Horse, Snake, etc.
Your Animal class will have the following instance variables:
private String name;
private double weight;
private int age;
Your Animal class will have the following constructors:
Animal()
Animal(String n, double weight, int age)
Your Animal class will have the following methods:
String makeNoise() - this will be an abstract method
double getWeight() - returns the weight of this animal
public String toString() - returns a String with information about this Animal
***************************************
You will have the following classes which all extend Animal:
Cow, Horse, Snake.
Cow will have an instance variable called:
private int num_spots
All variables should be initialized after either of the 2 constructors:
Cow()
Cow(String name, double weight, int age, int num_spots)
You will have the methods
String makeNoise() which returns "Moooo"
toString() - returns info about all variables including Animal things
**********************************************
Horse will have an instance variable called:
private double top_speed
All variables should be initialized after either of the 2 constructors:
Horse()
Horse(String name, double weight, int age, double top_speed)
You will have the methods
String makeNoise() which returns "Whinny"
toString() - returns info about all variables including Animal things
**********************************************
Snake will have an instance variable called:
private int num_fangs
All variables should be initialized after either of the 2 constructors:
Snake()
Snake(String name, double weight, int age, int num_fangs)
You will have the methods
String makeNoise() which returns "Hisssssss"
toString() - returns info about all variables including Animal things
**********************************************
You will have a Zoo class that contains the following instance variables:
private int actual_num_animals;
private int num_cages;
private Animal[] animals;
You will have the constructors:
Zoo() - default num_cages will be 20
Zoo(int num_cages)
You will have the following methods:
void add(Animal a) - adds an animal to your Zoo
double total_weight() - returns the total weight of all animals in the zoo
void make_all_noises() - Print out the noises made by all of the animals.
In otherwords, it calls the makeNoise() method for all animals in the zoo.
void print_all_animals() - prints the results of calling toString() on all animals in the zoo.
************************************************
Use the following main for your Zoo class:
public static void main(String[] args)
{
Zoo z = new Zoo();
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();
System.out.println("Total weight =" + z.total_weight());
System.out.println("**************************");
System.out.println("Animal Printout:");
z.print_all_animals();
}
Explanation / Answer
//Animal.java
public abstract class Animal {
private String name;
private double weight;
private int age;
public Animal() {
name = "";
weight = 0;
age = 0;
}
public Animal(String name, double weight, int age) {
this.name = name;
this.weight = weight;
this.age = age;
}
public abstract String makeNoise();
public double getWeight() {
return weight;
}
public String toString() {
return name + ", weight " + weight + ", age " + age;
}
}
//Cow.java
public class Cow extends Animal {
private int num_spots;
public Cow() {
super();
num_spots = 0;
}
public Cow(String name, double weight, int age, int num_spots) {
super(name, weight, age);
this.num_spots = num_spots;
}
@Override
public String makeNoise() {
return "Moooo";
}
public String toString() {
return "Cow " + super.toString() + ". Number of spots: " + num_spots;
}
}
//Horse.java
public class Horse extends Animal {
private double top_speed;
public Horse() {
super();
top_speed = 0.0;
}
public Horse(String name, double weight, int age, double top_speed) {
super(name, weight, age);
this.top_speed = top_speed;
}
@Override
public String makeNoise() {
return "Whinny";
}
public String toString() {
return "Horse " + super.toString() + ". Top speed: " + top_speed;
}
}
//Snake.java
public class Snake extends Animal {
private int num_fangs;
public Snake() {
super();
num_fangs = 0;
}
public Snake(String name, double weight, int age, int num_fangs) {
super(name, weight, age);
this.num_fangs = num_fangs;
}
@Override
public String makeNoise() {
return "Hisssssss";
}
public String toString() {
return "Snake " + super.toString() + ". Number of fangs: " + num_fangs;
}
}
//Zoo.java
public class Zoo {
private int actual_num_animals;
private int num_cages;
private Animal[] animals;
public Zoo() {
actual_num_animals = 0;
num_cages = 20;
animals = new Animal[num_cages];
}
public Zoo(int num_cages) {
actual_num_animals = 0;
this.num_cages = num_cages;
animals = new Animal[num_cages];
}
public void add(Animal a) {
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.");
}
}
public double total_weight() {
double totalWeight = 0.0;
for (int i = 0; i < actual_num_animals; i++) {
totalWeight += animals[i].getWeight();
}
return totalWeight;
}
public void make_all_noises() {
for (int i = 0; i < actual_num_animals; i++) {
System.out.println(animals[i].makeNoise());
}
}
public void print_all_animals() {
for (int i = 0; i < actual_num_animals; i++) {
System.out.println(animals[i]);
}
}
public static void main(String[] args) {
Zoo z = new Zoo();
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();
System.out.println("Total weight = " + z.total_weight());
System.out.println("**************************");
System.out.println("Animal Printout:");
z.print_all_animals();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.