Write an interactive Java program. Write a class called Animal that contains ins
ID: 3680603 • Letter: W
Question
Write an interactive Java program. Write a class called Animal that contains instance data the represents the animal’s name, age, type, and number of years equal to people years. Define the animal constructor to accept and initialize instance data. Include getter and setter methods for the name and age. Include a method to return the animals age in “person years” based on the age and the number of years equal to people years. Include a toString method that prints a sentence describing the animal. Create a driver class called Kennel, whose main method instantiates and updates several Animal objects, and uses the getter/setter methods as well as the toString and person years methods.
Requirements:
Explanation / Answer
class Animal{
String name;
int age;
String type;
int number_of_years;
Animal(String name,int age,String type,int number_of_years){
this.name=name;
this.age=age;
this.type=type;
this.number_of_years=number_of_years;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
public String getType(){
return type;
}
public int getNumberOfYears(){
return number_of_years;
}
public void setAge( int age){
this.age = age;
}
public void setName(String name){
this.name = name;
}
public void setType(String type){
this.type = type;
}
public void setNumberOfYears(int number_of_years){
this.number_of_years = number_of_years;
}
public int caluclateAnimalAge(int age,int number_of_years)
{
return age*int number_of_years;
}
public String toString(){
System.out.println("it is"+a.name+"of type"+a.type+"and"+a.age);
}
}
class Kennel
{
public static void main(String... s){
Animal a=new Animal("Tiger",5,"wild",10);
a.caluclateAnimalAge(5,10);
a.toString();
Animal a1=new Animal("Lion",7,"wild",10);
a1.caluclateAnimalAge(7,10);
a1.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.