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

thre wanted output : what i did which is wrong ! : instructor notes : / Create a

ID: 3728463 • Letter: T

Question

thre wanted output :

what i did which is wrong ! :

instructor notes :

/ Create an animal and print its info

// Parameters here are: Name, age, number of meals per day, and type (1

// = tiger, 2 = elephant, 3 = turtle, 4 = puffin)

There is a difference between the Type and ID attributes. In the example given by the Runner.java, the Elephant's ("Calvin") ID is indeed 1 but its Type is 2. Similarly, the Tiger's ("Fluffy") ID is 2 but its Type is 1.

While the ID should be automatically generated for each animal (in the order of creation), the type is always 1 for tigers and 2 for elephants. Moreover, if the Runner class had created a third animal (let's say another tiger), its ID is going to be 3 since it was the third animal created.

how do i create auto generated id in the class what i did was competly wrong ?

w .java. oce upe, Te outputo your poa the following: ID / Name Age CPD --------- 1) Calvin 5 960 (Elephant 4 320 (tiger) 320 ( Tiger 2 ) Fluffy Setting number of meals per day of Fluffy to 2 2 Fluffy 4640 Tiger Setting number of meals per day to 30! Error, animals cannot eat more than 3 meals per day! Fluffy 640 Ti Ger

Explanation / Answer

Please find the correct version of code below.

CODE

=====================

public class Animal {

   private int ID, age, numberOfMeals;

   private static int totalNumberOfAnimals = 0;

   private String name, typeN;

  

   public Animal() {

       totalNumberOfAnimals ++;

       ID = totalNumberOfAnimals;

       name = "Fluffy";

       typeN = "Tiger";

       age = 4;

       numberOfMeals = 1;

   }

  

   public Animal(String name, int age, int mealN, int typeN) {

       totalNumberOfAnimals ++;

       ID = totalNumberOfAnimals;

       this.name = name;

       this.age = age;

       this.numberOfMeals = mealN;

      

       switch(typeN) {

       case 1:

           this.typeN = "Tiger";

           break;

       case 2:

           this.typeN = "Elephant";

           break;

       case 3:

           this.typeN = "Turtle";

           break;

       case 4:

           this.typeN = "Puffin";

           break;

       default:

           this.typeN = "Invalid";

       }

   }

}

Explanation:

Instead of setting the ID based on typeN, we can use the value of totalNumberOfAnimals to set the value of ID.

The change which I am suggesting is to just set ID = totalNumberOfAnimals.