Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an interactive Java program. Write a class called Animal that contains ins

ID: 3681026 • 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.

class Animal2{

String name;

int age;

String type;

int number_of_years;

Animal2(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;

}

//Calculating the age of the animal

public int caluclateAnimalAge(int age,int number_of_years)

{

return age*number_of_years;

}

//ToString

public String toString(){

return "it is"+name+"of type"+type+"and"+age;

}

}

class Kennel2
{
public static void main(String[] args)
{


Animal2 a=new Animal2("Tiger",5,"wild",10);

a.caluclateAnimalAge(5,10);
a.toString();

Animal2 a1 = new Animal2("Lion",7,"wild",10);

a1.caluclateAnimalAge(7,10);
a1.toString();
}
}

This is what i have for my two .java files but i'm not getting an output when i run the program. i need help with constructing the string in the kennel2 class. should come out something like this.

Bob is a cat that is 5 years old, which is 20 human years old.
Bill is 14 human years old.
Bill is a dog that is 2 years old, which is 14 human years old.
Bill is now 3 years old.
Bill is a dog that is 3 years old, which is 21 human years old.
Bill is now named Rob.
Rob is a dog that is 3 years old, which is 21 human years old.
Ted is a squirrel that is 2 years old, which is 10 human years old.

Explanation / Answer

Hi, Please find below updated classes as per requirement. Please find highlighted changes in yur code.

Animal2.java


class Animal2{
   String name;
   int age;
   String type;
   int number_of_years;
   Animal2(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;
   }
   //Calculating the age of the animal
   public int caluclateAnimalAge(int age,int number_of_years)
   {
   return age*number_of_years;
     
   }
   //ToString
   public String toString(){
     
   return name+" is a "+type+" that is "+age+ " years old, which is "+caluclateAnimalAge(age, number_of_years)+" human years old";
   }

   }

Kennel2.java


public class Kennel2
{
   public static void main(String[] args)
   {

   Animal2 a=new Animal2("Bob",5,"Tiger",10);
   String t = a.toString();
   System.out.println(t);

   Animal2 a1 = new Animal2("Bill",7,"Dog",10);
   String s = a1.toString();
   System.out.println(s);

   }
   }

Output:

Bob is a Tiger that is 5 years old, which is 50 human years old
Bill is a Dog that is 7 years old, which is 70 human years old

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote