Question

Using the provided Animal class and Zoo class (found here), write a method in class Zoo that returns the average age for a gi

Provided code Animal.java:

public class Animal
{
   private String type;
   private double age;
  
   public Animal(String aT, double anA)
   {
       this.type = aT;
       if(anA >= 0)
       {
           this.age = anA;
       }
   }
   public String getType()
   {
       return this.type;
   }
   public double getAge()
   {
       return this.age;
   }
}

Provided code Zoo.java:

public class Zoo {
   private Animal[] animals;
   public void setAnimals(Animal[] a)
   {
       this.animals = a;
   }
  
   public double getAverageAge(String aType)
   {
//-----------------------------------------------
       //Write your code here

      
      
      
      
      
      
      
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


As per the problem statement I have solve the problem. Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)


import java.io.IOException;

public class Driver
{
    public static void main(String[] args) throws IOException {
        Animal animal = new Animal("Cat", "Kitty", 150.0, 10);
        Animal animal1 = new Animal("Rabbit", "Chiku", 100.5, 15);

        //Zoo class
        Zoo zoo = new Zoo();
        zoo.addAnimal(animal);
        zoo.addAnimal(animal1);

        // Print out info about the zoo:
        System.out.println(zoo);
    }
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Animal
{
    private String species;

    private String name;

    private double weight;

    private int age;

    //constructor
    public Animal()
    {
        this.species = "unknown";
        this.name = "noname";
        this.weight = 0.0;
        this.age = 0;
    }

    //animal constructor
    public Animal(String species, String name, double weight, int age)
    {
        this.species = species;
        this.name = name;
        this.weight = weight;
        this.age = age;
    }


    //get species
    public String getSpecies()
    {
        return this.species;
    }

    //get name
    public String getName()
    {
        return this.name;
    }

    //get weight
    public double getWeight()
    {
        return this.weight;
    }

    //get age
    public int getAge()
    {
        return this.age;
    }

   //toString method
    public String toString()
    {
        return String.format("%s, a %s. %.1f pounds, %d years old\n", this.name, this.species, this.weight, this.age);
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class Zoo
{
    private Animal[] animals;

    private int currentIndex;

    //construcctor
    public Zoo()
    {
        this.animals = new Animal[1];
        this.currentIndex = 0;
    }


    //expand capacity of zoo
    private void expandArray()
    {
        int oldSize = animals.length;
        Animal[] newArray = new Animal[oldSize + 2];
        for (int i = 0; i < oldSize; ++i)
        {
            newArray[i] = this.animals[i];
        }
        this.animals = newArray;
    }

    //method to add animal
    public void addAnimal(Animal ani)
    {
        if (this.currentIndex == this.animals.length)
        {
            this.expandArray();
        }
        this.animals[this.currentIndex] = ani;
        currentIndex++;
    }

    //get total wigth
    public double getTotalWeight()
    {
        double result = 0.0;
        // Loop over all animals and sum the weight:
        for (int i = 0; i < currentIndex; ++i)
        {
            result += this.animals[i].getWeight();
        }
        return result;
    }

    //get average age
    public int getAverageAge()
    {
        if (currentIndex == 0)
        {
            return 0;
        }
        double totalAge = 0;
        for (int i = 0; i < this.currentIndex; i++)
        {
            Animal animal = this.animals[i];
            totalAge += animal.getAge();
        }
        return (int) (totalAge / currentIndex);
    }

    //get size
    public int getSize()
    {
        return this.currentIndex;
    }

    //get total age with name
    public int totalAgeWithName(String name)
    {
        int totalAge = 0;
        for (int i = 0; i < this.currentIndex; ++i)
        {
            // Only add age if the animal has the same name.
            Animal animal = this.animals[i];
            if (animal.getName().equalsIgnoreCase(name))
            {
                totalAge += animal.getAge();
            }
        }
        return totalAge;
    }

    //get animal
    public Animal[] getAnimals()
    {
        return animals;
    }

    //to string method
    public String toString()
    {
        String result = "Animals currently reside in the zoo: \n";
        for (int i = 0; i < this.currentIndex; ++i)
        {
            result += this.animals[i].toString();
        }
        return result;
    }
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Program Output:



Zoo [/IdeaProjects/Zoo) -.../src/Driver.java (Zoo] Main - the Q Zoo src Driver - Project - Zoo -/IdeaProjects/Zoo idea out sr

Add a comment
Know the answer?
Add Answer to:
Provided code Animal.java: public class Animal {    private String type;    private double age;   ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Here is the code for the Infant class: public class Infant{ private String name; private int...

    Here is the code for the Infant class: public class Infant{ private String name; private int age; // in months public Infant(String who, int months){ name = who; age = months; } public String getName(){ return name;} public int getAge(){ return age;} public void anotherMonth() {age = age + 1;} } The code box below includes a live Infant array variable, thoseKids. You cannot see its declaration or initialization. Your job is to find out which Infant in the array...

  • public class Animal {    private String name; //line 1    private int weight; //line 2...

    public class Animal {    private String name; //line 1    private int weight; //line 2    private String getName(){       return name;    } //line 3    public int fetchWeight(){       return weight; } //line 4 } public class Dog extends Animal {    private String food; //line 5    public void mystery(){       //System.out.println("Name = " + name); //line 6            System.out.println("Food = " + food); //line 7    } } I want to know the super...

  • help with java OOP, here is the started code: package P2; public class Farm {   ...

    help with java OOP, here is the started code: package P2; public class Farm {    private double availableFood;    private Animal[] animals;    public Farm() {        setAvailableFood(1000);        animals = new Animal[4];        animals[0] = new Chicken();        animals[1] = new Cow();        animals[2] = new Llama();        animals[3] = new Llama();    }    public void makeNoise(){           // all animals make their sound (Moo, Cluck, etc)        for(Animal...

  • import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public...

    import java.util.Scanner; public class Cat { private String name; private Breed breed; private double weight; public Cat(String name, Breed breed, double weigth) { this.name = name; this.breed = breed; this.weight = weight; } public Breed getBreed() { return breed; } public double getWeight() { return weight; } //other accessors and mutators ...... } public class Breed { private String name; private double averageWgt; public Breed(String name,double averageWgt) { this.name = name; this.averageWgt= averageWgt; } public double getWeight() { return averageWgt;...

  • java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ========= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

  • Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models...

    Solve this using Java for an Intro To Java Class. Provided files: Person.java /** * Models a person */ public class Person { private String name; private String gender; private int age; /** * Consructs a Person object * @param name the name of the person * @param gender the gender of the person either * m for male or f for female * @param age the age of the person */ public Person(String name, String gender, int age) {...

  • Hello everybody. Below I have attached a code and we are supposed to add comments explaining...

    Hello everybody. Below I have attached a code and we are supposed to add comments explaining what each line of code does. For each method add a precise description of its purpose, input and output.  Explain the purpose of each member variable. . Some help would be greaty appreciated. (I have finished the code below with typing as my screen wasnt big enough to fit the whole code image) } public void withdrawal (double amount) { balance -=amount; }...

  • Java. What is the output of this code? Ace.java public class Ace { private double a;...

    Java. What is the output of this code? Ace.java public class Ace { private double a; public Ace ( double x) {       a = x; } public void multiply (double f) {       a *= x;         } public String toString () {       return String.format ("%.3f", x); AceDem.java public class AceDemo { public static void main(String args[]) {    Ace card = new Ace (2.133); card.multiply (.5); System.out.println (card); } }

  • Question 19 Given the following class: public class Swapper ( private int x; private String y...

    Question 19 Given the following class: public class Swapper ( private int x; private String y public int z; public Swapper( int a, String b, int c) ( x-a; y b; zC; public String swap() ( int temp -x; x-z z temp; return y: public String tostring() ( if (x<z) return y: else return" +x+z What would the following code output? Swapper r new Suapper( 5, "no", 10); System.out.printin( r. swap ) ): no no510 510 e 15 Question 20...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT