Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions