Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

public class Animal private String commonName private String habitat privat... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

More Books

Students also viewed these Programming questions

Question

Java source files end with the . class

Answered: 1 week ago

Question

Using a graphing utility, graph y = cot -1 x.

Answered: 1 week ago