How do I fix the errors? Java Error 1 overloading constructor with invalid weight ERROR: Couldn't correctly retrieve attributes of an Animal object created with invalid weight Error 2 overloading constructor with invalid gender ERROR: Couldn't correctly retrieve attributes of an Animal object created with invalid gender Code: public class Animal { private String name; private char gender; private int birthYear; private double weight; public Animal() { birthYear = 1900; name = ""; gender = 'u'; weight = 0.0; } public Animal(String name, int birthYear, double weight, char gender) { this.birthYear = birthYear; this.name = name; this.gender = gender; this.weight = weight; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getBirthYear() { return birthYear; } public void setBirthYear(int birthYear) { this.birthYear = birthYear; } public double getWeight() { return weight; } public void setWeight(double weight) { if (weight < 0 ) { this.weight = -1.0; } else { this.weight = weight; } } public char getGender() { return gender; } public void setGender(char gender) { if ( (gender == 'f') || (gender == 'm') || (gender == 'u') ) { this.gender = gender; } else { this.gender = 'u'; } } public int calculateAge(int currentYear) { return(currentYear < this.getBirthYear() ? -1 : (currentYear - this.getBirthYear())); } public boolean isMale() { return(gender == 'm'); } public boolean isFemale() { return (gender == 'f'); } // Specify the arguments in the String.format public void printDetails() { System.out.println(String.format("Name: %20s | Year of Birth: %4d | Weight: %10.2f | Gender: %c\n",name,birthYear,weight,gender)); } public void gainWeight() { weight += 1; } public void gainWeight(double amount) { if (amount <= 0) return; weight += amount; } public void loseWeight() { if ((weight - 1) >= 0) { weight -= 1; } } public void loseWeight(double amount) { if ((weight - amount) >= 0) weight -= amount; } }
How do I fix the errors? Java
Error 1
overloading constructor with invalid weight
ERROR: Couldn't correctly retrieve attributes of an Animal object created with invalid weight
Error 2
overloading constructor with invalid gender
ERROR: Couldn't correctly retrieve attributes of an Animal object created with invalid gender
Code:
public class Animal {
private String name;
private char gender;
private int birthYear;
private double weight;
public Animal() {
birthYear = 1900;
name = "";
gender = 'u';
weight = 0.0;
}
public Animal(String name, int birthYear, double weight, char gender) {
this.birthYear = birthYear;
this.name = name;
this.gender = gender;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBirthYear() {
return birthYear;
}
public void setBirthYear(int birthYear) {
this.birthYear = birthYear;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
if (weight < 0 ) {
this.weight = -1.0;
}
else {
this.weight = weight;
}
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
if ( (gender == 'f') || (gender == 'm') || (gender == 'u') ) {
this.gender = gender;
}
else {
this.gender = 'u';
}
}
public int calculateAge(int currentYear) {
return(currentYear < this.getBirthYear() ? -1 : (currentYear - this.getBirthYear()));
}
public boolean isMale() {
return(gender == 'm');
}
public boolean isFemale() {
return (gender == 'f');
}
// Specify the arguments in the String.format
public void printDetails() {
System.out.println(String.format("Name: %20s | Year of Birth: %4d | Weight: %10.2f | Gender: %c\n",name,birthYear,weight,gender));
}
public void gainWeight() {
weight += 1;
}
public void gainWeight(double amount) {
if (amount <= 0)
return;
weight += amount;
}
public void loseWeight() {
if ((weight - 1) >= 0) {
weight -= 1;
}
}
public void loseWeight(double amount) {
if ((weight - amount) >= 0)
weight -= amount;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps