public class Animal { private String name; private int birthYear; private double weight; private char gender; public Ani

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

public class Animal { private String name; private int birthYear; private double weight; private char gender; public Ani

Post by answerhappygod »

public class Animal {private String name;private int birthYear;private double weight;private char gender;public Animal() { name = ""; birthYear = 1900; weight = 0.0; gender = 'u'; }public Animal(String name, int birthYear, double weight, char gender) { this.name = name; this.birthYear = birthYear; this.weight = weight; this.gender = gender;}
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 = weight;else this.weight = -1;}
public char getGender() {return gender;}
public void setGender(char gender) {if (gender == 'm' || gender == 'f')this.gender = gender;else this.gender = 'u';}
public int calculateAge(int year) {if (year < birthYear) return -1;return year - birthYear;}
public boolean isMale() {return gender == 'm';}
public boolean isFemale() {return !isMale();}
public void printDetails() {System.out.printf("Name:%20s | Year of Birth: %d | Weight: %10.2f | Gender: %c\n",getName(), getBirthYear(), getWeight(), getGender());}
public void gainWeight() {weight += 1;}
public void gainWeight(double weight) {if (weight > 0) this.weight += weight;}
public void loseWeight() {weight -= 1;if (weight < 0) weight = 0;}
public void loseWeight(double weight) {this.weight -= weight;if (this.weight < 0) this.weight = 0;}
@Override
public String toString() {return this.getName() + ", Year Born: " + getBirthYear() + ", Weight: " + getWeight() + ", Gender: " + getGender();}}
public class Driver {
public static void main(String[] args) { Farm f1 = new Farm(10);
Animal a1 = new Animal("cow", 2012, 100.5, 'f'); Animal a2 = new Animal("pig", 2009, 550.5, 'm'); Animal a3 = new Animal("donkey", 1999, 773.42, 'm'); Animal a4 = new Animal("sheep", 2016, 164.23, 'f'); Animal a5 = new Animal("goose", 2004, 10.75, 'f');
f1.addAnimal(a1); f1.addAnimal(a2); f1.addAnimal(a3); f1.addAnimal(a4); f1.addAnimal(a5);
f1.printAllDetails(); }}
PLEASE HELP FIX THESE ERRORS:
ERROR: Couldn't correctly retrieve attributes of an Animal object created with invalid weight
ERROR: Couldn't correctly retrieve attributes of an Animal object created with invalid gender
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply