Answered step by step
Verified Expert Solution
Question
1 Approved Answer
OUTPUT should be like this please FarmName: farm of ten | Number of Animals: 5 | Farm Sizpublic class Farm { private String farmName; private
OUTPUT should be like this please
FarmName: farm of ten Number of Animals: Farm Sizpublic class Farm
private String farmName;
private Animal animals;
private int numAnimals;
public static void mainString args
Farm farm new Farm;
Animal lion new Animallionf;
lion.printDetails;
farm new Farm;
farmaddAnimallion;
public Farm
animals new Animal;
farmName ;
numAnimals ;
public FarmString farmName
this.farmName farmName;
animals new Animal;
numAnimals ;
public Farmint maxAnimals
animals new AnimalmaxAnimals maxAnimals : ;
farmName "farm of maxAnimals;
numAnimals ;
public Farmjavalang.String farmName, int maxAnimals
this.farmName farmName;
animals new AnimalmaxAnimals maxAnimals : ;
numAnimals ;
public void setFarmNameString name
farmName name;
public String getFarmName
return farmName;
public void addAnimalAnimal a
if numAnimals animals.length
resizeAnimalArray;
animalsnumAnimals a;
numAnimals ;
public Animal getAnimalint index
if index numAnimals index
return null;
else
return animalsindex;
public int getNumAnimals
return numAnimals;
public Animal getFirstAnimal
return getAnimal;
public Animal getLastAnimal
return getAnimalnumAnimals;
public Animal getAnimals
return animals;
public Animal removeAnimalint index
Animal removedAnimal null;
if index && index numAnimals
removedAnimal animalsindex;
for int i index; i numAnimals ; i
animalsi animalsi;
animalsnumAnimals null;
numAnimals ;
return removedAnimal;
public void removeAllAnimals
for int i ; i numAnimals; i
animalsi null;
numAnimals ;
public double getTotalWeightOfAllAnimals
double sum ;
for int i ; i numAnimals; i
Animal a animalsi;
sum agetWeight;public class Animal
private String name;
private char gender;
private int birthYear;
private double weight;
public Animal
birthYear ;
name ;
gender u;
weight ;
public AnimalString 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 setNameString name
this.name name;
public int getBirthYear
return birthYear;
public void setBirthYearint birthYear
this.birthYear birthYear;
public double getWeight
return weight;
public void setWeightdouble weight
if weight
this.weight ;
else
this.weight weight;
public char getGender
return gender;
public void setGenderchar gender
if gender fgender mgender u
this.gender gender;
else
this.gender u;
public int calculateAgeint currentYear
returncurrentYear this.getBirthYear : currentYear this.getBirthYear;
public boolean isMale
returngender m;
public boolean isFemale
return gender f;
Specify the arguments in the String.format
public void printDetails
System.out.printlnStringformatName: s Year of Birth: d Weight: f Gender: c
name,birthYear,weight,gender;
public void gainWeight
weight ;
public void gainWeightdouble amount
if amount
return;
weight amount;
public void loseWeight
if weight
weight ;
public void loseWeightdouble amount
if weight amount
weight amount;
public class Driver
main method
public static void mainString args
Create a Farm of size
Farm farm new Farm;
Create Animal Objects with the details specified in the table below
Animal a new Animalcowf;
Animal a new Animalpigm;
Animal a new Animaldonkeym;
Animal a new Animalsheepf;
Animal a new Animalgoosef;
Add the Animal objects to the Farm
farm.addAnimala;
farm.addAnimala;
farm.addAnimala;
farm.addAnimala;
farm.addAnimala;
Call the printDetails method from the Farm to print all the Farm and Animal details.
farm.printAllDetails;
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