i am asked to create an animal constructor with the following parameters Paramet
ID: 3727111 • Letter: I
Question
i am asked to create an animal constructor with the following parameters
Parameters here are: Name, age, number of meals per day, and type (1 = tiger, 2 = elephant, 3 = turtle, 4 = puffin)
how can i do the type ?
here is what i did and i'm sure its wrong i dont want system.out.print ? how can i do it ?
public class Animal [ public static String totalNumOfAnimals; private String Name; private int Age; private int NumOfMeals; private int typeN; public Animal ublic Animal (String name, int age, int mealsN, int typeN) super Name = name; Age = age ; this. NumOfMeals me alsN; switch (typeN) case 1: System.out.println("tiger"); break case 2: System.out.println("elephant"); break case 3 System.out.println("turtle"); break case 4: System.out.println ("puffin"); break this. typeN = typeN;Explanation / Answer
Hello, I have a solution for you.
Your problem can be solved using many ways. I’m presenting you two different approaches.
1) Changing the datatype of type attribute of Animal class to String, and accepting an integer value in the constructor for the type, switching the integer value, if it is 1, setting the type as “tiger”, if it is 2, setting the type as “elephant”, “turtle” if 3, and “puffin” if 4. And in case of any other values, setting the type as “tiger”.
//Animal.java
public class Animal {
public static String totalNumOfAnimals;
private String Name;
private int Age;
private int NumMeals;
private String type;
public Animal() {
}
public Animal(String name, int age, int numMeals, int typeN) {
Name = name;
Age = age;
NumMeals = numMeals;
//switching the typeN
switch (typeN) {
case 1://setting type to tiger
type = "tiger";
break;
case 2://setting type to elephant
type = "elephant";
break;
case 3://setting type to turtle
type = "turtle";
break;
case 4://setting type to puffin
type = "puffin";
break;
default:
type = "tiger"; // default value
break;
}
}
/**
* returns a String containing all data
*/
public String toString() {
return "Name: " + Name + ", Age: " + Age + ", Number of meals: "
+ NumMeals + ", Type: " + type;
}
}
//Test.java - a driver class for testing the Animal class
public class Test {
public static void main(String[] args) {
Animal animal1 = new Animal("Jimmy", 6, 3, 1);// a tiger
Animal animal2 = new Animal("Rocky", 20, 3, 3); // a turtle
/**
* Displaying both animals, the toString() method will get invoked
* automatically
*/
System.out.println(animal1);
System.out.println(animal2);
}
}
//output
Name: Jimmy, Age: 6, Number of meals: 3, Type: tiger
Name: Rocky, Age: 20, Number of meals: 3, Type: turtle
2) Using an enumeration for the type. In the below solution, I have defined an enumeration for the Type, which can contain any one of the values- tiger, elephant, turtle or puffin. And then changed the datatype of type attribute to Type, so that only valid values can be used.
//Animal.java
public class Animal {
public static String totalNumOfAnimals;
private String Name;
private int Age;
private int NumMeals;
//datatype of type changed to Type enumeration
private Type type;
public Animal() {
}
public Animal(String name, int age, int numMeals, Type type) {
Name = name;
Age = age;
NumMeals = numMeals;
// the type will be either tiger, elephant, turtle or puffin
this.type = type;
}
/**
* returns a String containing all data
*/
public String toString() {
return "Name: " + Name + ", Age: " + Age + ", Number of meals: "
+ NumMeals + ", Type: " + type;
}
}
/**
* enumeration defined to represent all possible values for the animal type, defined within Animal.java
*/
enum Type {
tiger, elephant, turtle, puffin
}
//Test.java
public class Test {
public static void main(String[] args) {
Animal animal1 = new Animal("Jimmy", 6, 3, Type.tiger);// a tiger
Animal animal2 = new Animal("Rocky", 20, 3, Type.turtle); // a turtle
/**
* Displaying both animals, the toString() method will get invoked
* automatically
*/
System.out.println(animal1);
System.out.println(animal2);
}
}
//output
Name: Jimmy, Age: 6, Number of meals: 3, Type: tiger
Name: Rocky, Age: 20, Number of meals: 3, Type: turtle
If you still have any doubts, feel free to drop a comment. Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.