Question: Complete the toString method in the Animal Class. Remember that the format for a toString method is: public String toString() The toString method should be

Complete the toString method in the Animal Class. Remember that the format for a toString method is:

public String toString()

The toString method should be formatted so that the following code

Animal riverOtter = new Animal("North American River Otter", "rivers", "fish", 9); System.out.println(riverOtter);

Prints out

North American River Otter lives in rivers, eats fish and usually lives 9 years.

 

Main.java starter code:

public class Main
{
    public static void main(String[] args)
    {
       Animal riverOtter = new Animal("North American River Otter", "rivers", "fish", 9);
       System.out.println(riverOtter);
    }
}

 

Animal.java starter code:

public class Animal
{
    private String commonName;
    private String habitat;
    private String diet;
    private int typicalAge;
   
   
    public Animal(String name, String animalHabitat, String animalDiet, int age)
    {
        commonName = name;
        habitat = animalHabitat;
        diet = animalDiet;
        typicalAge = age;
    }
   
    public String toString()
    {
       
    }
}

Step by Step Solution

3.39 Rating (158 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public class Animal private String commonName private String habitat privat... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!