Answered step by step
Verified Expert Solution
Link Copied!

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: 5| Farm Sizpublic class Farm{
private String farmName;
private Animal[] animals;
private int numAnimals;
public static void main(String[] args){
Farm[] farm = new Farm[10];
Animal lion = new Animal("lion",1999,12,'f');
lion.printDetails();
farm[0]= new Farm();
farm[0].addAnimal(lion);
}
public Farm(){
animals = new Animal[10];
farmName ="";
numAnimals =0;
}
public Farm(String farmName){
this.farmName = farmName;
animals = new Animal[10];
numAnimals =0;
}
public Farm(int maxAnimals){
animals = new Animal[maxAnimals >0? maxAnimals : 0];
farmName = "farm of "+ maxAnimals;
numAnimals =0;
}
public Farm(java.lang.String farmName, int maxAnimals){
this.farmName = farmName;
animals = new Animal[maxAnimals >0? maxAnimals : 0];
numAnimals =0;
}
public void setFarmName(String name){
farmName = name;
}
public String getFarmName(){
return farmName;
}
public void addAnimal(Animal a){
if (numAnimals >= animals.length){
//resizeAnimalArray();
}
animals[numAnimals]= a;
numAnimals +=1;
}
public Animal getAnimal(int index){
if (index > numAnimals || index <0){
return null;
}
else {
return animals[index];
}
}
public int getNumAnimals(){
return numAnimals;
}
public Animal getFirstAnimal(){
return getAnimal(0);
}
public Animal getLastAnimal(){
return getAnimal(numAnimals-1);
}
public Animal[] getAnimals(){
return animals;
}
public Animal removeAnimal(int index){
Animal removedAnimal = null;
if (index >=0 && index < numAnimals){
removedAnimal = animals[index];
for (int i = index; i < numAnimals -1; i++){
animals[i]= animals[i+1];
}
animals[numAnimals-1]= null;
numAnimals -=1;
}
return removedAnimal;
}
public void removeAllAnimals(){
for (int i =0; i < numAnimals; i++){
animals[i]= null;
}
numAnimals =0;
}
public double getTotalWeightOfAllAnimals(){
double sum =0.0;
for (int i =0; i < numAnimals; i++){
Animal a = animals[i];
sum += a.getWeight();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
",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;
}
}public class Driver {
/**
* main method
*/
public static void main(String[] args){
//1. Create a Farm of size 10
Farm farm = new Farm(10);
//2. Create 5 Animal Objects with the details specified in the table below
Animal a1= new Animal("cow",2012,1000.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');
//3. Add the 5 Animal objects to the Farm
farm.addAnimal(a1);
farm.addAnimal(a2);
farm.addAnimal(a3);
farm.addAnimal(a4);
farm.addAnimal(a5);
//4. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions