Question: public class Animal { private String name; private int birthYear; private double weight; private char gender; / / Default constructor Animal ( ) { this.name

public class Animal {
private String name;
private int birthYear;
private double weight;
private char gender;
// Default constructor
Animal(){
this.name ="";
this.birthYear =1900;
this.weight =0;
this.gender ='u';
}
// Parameterized constructor
Animal(String name, int year, double weight, char gender){
this.name = name;
this.birthYear = year;
this.weight = weight;
this.gender = gender;
}
public String getName(){
return name;
}
public int getBirthYear(){
return birthYear;
}
public double getWeight(){
return weight;
}
public char getGender(){
return gender;
}
public void setName(String name){
this.name = name;
}
public void setBirthYear(int birthYear){
this.birthYear = birthYear;
}
public void setWeight(double weight){
if (weight <0)
this.weight =-1;
else
this.weight = weight;
}
public void setGender(char gender){
if (gender !='m' && gender !='f')
this.gender ='u';
else
this.gender = gender;
}
public int calculateAge(int current){
if (current < this.birthYear)
return -1;
return current - this.birthYear;
}
public boolean isMale(){
return this.gender =='m';
}
public boolean isFemale(){
return this.gender =='f';
}
public void gainWeight(){
this.weight++;
}
public void gainWeight(double x){
if (x >=0)
this.weight += x;
}
public void loseWeight(){
this.weight = Math.max(0, this.weight -1);
}
public void loseWeight(double x){
if (x >=0){
this.weight = Math.max(0, this.weight - x);
}
}
public void printDetails(){
String name = String.format("Name: %20s", this.name);
String year = String.format("Year of Birth: %4d", this.birthYear);
String w = String.format("Weight: %10.2f", this.weight);
String g = String.format("Gender: %c", this.gender);
System.out.println(name +"|"+ year +"|"+ w +"|"+ g);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!