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
Step: 1
public class Animal private String commonName private String habitat privat...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started