Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public class Farm { private String farmName; private Animal [ ] animals; private int numAnimals; public static void main ( String [ ] args )
public 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;
return sum;
public double getAverageWeightOfAllAnimals
double sum getTotalWeightOfAllAnimals;
return sum numAnimals;
public int getNumberOfAnimalsAboveWeightdouble weight
int numOfAboveAvg ;
for int i ; i numAnimals; i
if animalsigetWeight weight
numOfAboveAvg;
return numOfAboveAvg;
public int getNumberOfAnimalsBelowWeightdouble weight
int numOfBelowAvg ;
for int i ; i numAnimals; i
if animalsigetWeight weight
numOfBelowAvg;
return numOfBelowAvg;
public void increaseWeightOfAllAnimals
for int i ; i numAnimals; i
animalsigainWeight;
public void increaseWeightOfAllAnimalsdouble weight
for int i ; i numAnimals; i
animalsigainWeightweight;
public void printAllDetails
int length animals.length;
System.out.printfFarmName: s Number of Animals: d Farm Size: d
farmName, numAnimals, length;
for int i ; i numAnimals; i
animalsiprintDetails;
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