How do I fix the errors? 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;    } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

How do I fix the errors?

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;
   }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education