public ctass Runner public static void main(Stringt) args) t //print top header
ID: 3727198 • Letter: P
Question
public ctass Runner public static void main(Stringt) args) t //print top header Animal.printHeader); //Create an animat and print its info //Parameters here are: Nane, age, number of meals per day, and type (1 tiger, 2 elephant, 3- turtle, 4 puffin) //Pay attention that the ID is auto generated Animal a1 new Animat("Calvin", 5, 3, 2) //Print info will print the ID, nane, Age, Calories Per Day (CPD), and Type //Calories Per Day is the number of meals the aninal eats32 a1.printAnimalInfo)i System.out.println): //Create second animal and print its info * Default values are: * Name "Fluffy"; * Age . * Type 1; e Number of Meals 1: * Type-1 (tiger); Animal a2 new Animal: a2. printAninalInfol): System.out.println) //set number of meals per day for Fluffy to 2 System.out.printin( setting number of meals per day of Flufty to 2") a2.printAnimalInfo); System.out.printin) //Set number of meals per day for Fluffy to 30! System.out.printIn("Setting number of neals per day to 30"): a2. setNumOfMeals (30): a2.printAnimalInfo); System.out.println) //Set the age of Fluffy to 8 System.out.println( "Setting Fluffy's age to 8") a2. setAge(8); a2.printAnimalInfo); System.out.print Ln) //print all animals in system System.out.println("InPrinting All Animals:n") Animal.printHeader); al.printAnimalInfo); a2.printAnimalInfo); System.out.printLn("inTotal number of animals is: " Animal.totaLNumOfAnimals);Explanation / Answer
class Animal
{
private String name;
private int age,type,numOfMeals;
private static int counter = 0;
private int id;
public static int totalNumOfAnimals = 0; // public static variable
public Animal() //default constructor
{
name = "Fluffy";
age = 4;
type = 1;
numOfMeals = 1;
totalNumOfAnimals++;
id = ++counter;
}
public Animal(String name,int age,int type,int numOfMeals)// argument constructor
{
this.name = name;
this.age = age;
this.type = type;
this.numOfMeals = numOfMeals;
totalNumOfAnimals++;
id = ++counter;
}
public static void printHeader()
{
System.out.println("Welcome in the Zoo");
}
public void printAnimalInfo()
{
String typeName = " ";
if(type == 1)
typeName = "tiger";
else if(type == 2)
typeName = "elephant";
else if(type == 3)
typeName = "turtle";
else if(type == 4)
typeName = "puffin";
System.out.println("Animal ID : "+id);
System.out.println("Name : "+name);
System.out.println("Age : "+age);
System.out.println("Calories Per Day : "+ (numOfMeals *320));
System.out.println("Type :"+typeName);
}
public void setNumOfMeals(int meals)
{
numOfMeals = meals;
}
}
class Runner
{
public static void main (String[] args)
{
Animal.printHeader();
Animal a1 = new Animal("Calvin",5,3,2);
a1.printAnimalInfo();
System.out.println();
Animal a2 = new Animal();
a2.printAnimalInfo();
System.out.println();
System.out.println("Setting number of meals per day of Fluffy to 2");
a2.setNumOfMeals(2);
a2.printAnimalInfo();
System.out.println();
System.out.println("Setting number of meals per day of Fluffy to 30");
a2.setNumOfMeals(30);
a2.printAnimalInfo();
System.out.println();
System.out.println("Setting number of meals per day of Fluffy to 8");
a2.setNumOfMeals(8);
a2.printAnimalInfo();
System.out.println();
System.out.println(" Printing All Animals: ");
Animal.printHeader();
a1.printAnimalInfo();
a2.printAnimalInfo();
System.out.println(" Total Number of Animals : "+Animal.totalNumOfAnimals);
}
}
Output:
Welcome in the Zoo
Animal ID : 1
Name : Calvin
Age : 5
Calories Per Day : 640
Type :turtle
Animal ID : 2
Name : Fluffy
Age : 4
Calories Per Day : 320
Type :tiger
Setting number of meals per day of Fluffy to 2
Animal ID : 2
Name : Fluffy
Age : 4
Calories Per Day : 640
Type :tiger
Setting number of meals per day of Fluffy to 30
Animal ID : 2
Name : Fluffy
Age : 4
Calories Per Day : 9600
Type :tiger
Setting number of meals per day of Fluffy to 8
Animal ID : 2
Name : Fluffy
Age : 4
Calories Per Day : 2560
Type :tiger
Printing All Animals:
Welcome in the Zoo
Animal ID : 1
Name : Calvin
Age : 5
Calories Per Day : 640
Type :turtle
Animal ID : 2
Name : Fluffy
Age : 4
Calories Per Day : 2560
Type :tiger
Total Number of Animals : 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.