Question
Java Programming II Questions Homework 4-1 https://che.gg/2ILXRYA Animal - height : double + Animal(String, int, double, double, char) //name, birthYear, weight, height, gender + getHeight()
Java Programming II Questions
Homework 4-1
https://che.gg/2ILXRYA
Animal - height : double + Animal(String, int, double, double, char) //name, birthYear, weight, height, gender + getHeight() : double + setHeight(double) : void //if input value is negative set height to -1 + calculateBMI() : double //using pounds and inches BMI = weight / (height2) * 703 + printDetails() : void // prints Animal attributes in the following format: // "Name: %20s | Year of Birth: %4d | Weight: %10.2f | Height: %10.2f | Gender: %c " + toString() : String @Override //see Note1 for format + equals(Object) : boolean @Override //see Note2 for equality checks NOTE1: Format the data for the toString() method as "Name: %20s | Year of Birth: %4d | Weight: %10.2f | Height: %10.2f | Gender: %c " NOTE2: For the equals(Object) method, two Animal objects are considered equal if both have the same values for birthYear, gender, name and if their weight, height values are within .5 of the others weight, height values
//ANIMAL.JAVA
public class Animal {
private String name;
private int birthYear ;
private double weight ;
private char gender;
private double height;
public Animal()
{
name="";
birthYear=1900;
weight=0.0;
height=0.0;
gender='u';
}
public Animal(String n, int b, double w,double h, char g)
{
name=n;
birthYear=b;
setWeight(w);
setGender(g);
setHeight(h);
// if(w >= 0) weight=w;
// else weight=0;
// if(g == 'm' || g == 'f')
// gender = g;
// else
// gender = 'u';
}
public String getName()
{
return name;
}
public void setName(String n)
{
name =n;
}
public int getBirthYear()
{
return birthYear;
}
public void setBirthYear(int birthYear)
{
this.birthYear = birthYear;
}
public double getWeight()
{
return weight;
}
public void setWeight(double w)
{
if(Double.compare(w, 0.0) < 0)
{ weight = -1.0;
return;
}
else{
weight = w ; }
}
public double getHeight()
{
return height;
}
public void setHeight(double h)
{
if(Double.compare(h, 0.0) < 0)
{
height = -1.0;
return;
}
else{
height = h ;
}
}
public char getGender()
{
return gender;
}
public void setGender(char g)
{
if(g == 'm' || g == 'f')
gender = g;
else
gender = 'u';
}
public double calculateBMI()
{
if(height < 0 || weight < 0)
return -1;
return(weight/(Math.pow(height,2)*703));
}
public int calculateAge(int currentYear)
{
if(currentYear < birthYear)
return -1;
return currentYear-birthYear;
}
public boolean isMale()
{
return gender=='m';
}
public boolean isFemale()
{
return gender=='f';
}
public void printDetails()
{
System.out.printf("Name: %20s | Year of Birth: %4d | Weight: %10.2f | Height: %10.2f | Gender: %c ",name,birthYear,weight,height,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;
}
public String toString()
{
return String.format("Name: %20s | Year of Birth: %4d | Weight: %10.2f | Height: %10.2f | Gender: %c ",name,birthYear,weight,height,gender);
}
@Override
public boolean equals(Object obj)
{
if(obj instanceof Animal)
{
if((birthYear == ((Animal)obj).birthYear) && (gender == ((Animal)obj).gender) && (name.equals(((Animal)obj).name))
&& (Math.abs(height - ((Animal)obj).height) <= 0.5) && (Math.abs(weight - ((Animal)obj).weight) <= 0.5))
return true;
}
return false;
}
}
//END OF ANIMAL.JAVA
Multiple Errors!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!
overloading constructor
Compilation failed
zyLabsUnitTest.java:11: error: no suitable constructor found for Animal(String,int,double,char) Animal animal2 = new Animal("Tiger", 2008, 123.45, 'f'); constructor Animal.Animal() is not applicable (actual and formal argument lists differ in length) constructor Animal.Animal(String,int,double,double,char) is not applicable (actual and formal argument lists differ in length) 1 error
overloading constructor with invalid weight
Compilation failed
overloading constructor with invalid gender
Compilation failed
setName()
Compilation failed
setBirthYear()
Compilation failed
setGender() with invalid input
Compilation failed
setGender() with valid input
Compilation failed
calculateAge(int)
Compilation failed
calculateAge(int) with currentYear < birthYear
Compilation failed
isMale()
Compilation failed
isFemale()
Compilation failed
gainWeight()
Compilation failed
gainWeight(double)
Compilation failed
loseWeight()
Compilation failed
loseWeight(double)
Compilation failed
gainWeight(double) with weight decrease below 0
Compilation failed
loseWeight() with weight decrease below 0
Compilation failed
loseWeight(double) with weight decrease below 0
Compilation failed
calculateBMI() method in Animal class
Your output:
calculateAge(int) does not work properly with valid height and weight
(You are awesome Chegg Q&A! Instructors of the year in my book )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started