Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Simulating a Turkey Farm We have two classes that are going to be created, Turke

ID: 3762908 • Letter: S

Question

Simulating a Turkey Farm

We have two classes that are going to be created, TurkeyFarm.java and Turkey.java. A skeleton has been provided for Turkey. Provide implementations for the following methods:

birth(). This method will set the current Turkey's pregnancy status to false if not already false.

isAdult(). This method returns true if age is over the adult age

isPregnant(). This method returns true if pregnant.

istimeToDie(). This method returns true if age is over the max age.

getID(). This method returns the ID of the Turkey.

getPairID(). This method returns the ID of the turkey it is paired with.

unPair(). This method removes the current value for who it is paired with and sets it to 0.

passTime(). This method will add to each of the day values. This includes the age and pregnancy.

slaughter(). This method will increment a counter that keeps track of how many Turkeys have been slaughtered so far.

isPaired(). This method returns true if the turkey's pairID is not equal to zero.

mate(Turkey t1, Turkey t2). This method will determine which of the two is female. Then it will make the female pregnant and set the days pregnant to 0.

pair(Turkey t1, Turkey t2). This method will set the pairID of each turkey to the ID of the other.

getGender(). This method returns the number for the gender, either 1 or 2.

isPregnancyDue(). This method returns true if the daysPregnant is greater than Pregnancy duration.

The constructor for the Turkey has not been implemented. It should include initialize all the variables for a single turkey as well as change any necessary static variables. Be sure to use the random number generator to randomly assign genders.

Methods not written but need to be implemented.

toString(). This method will return a string with all of the relevant information about a turkey.

Static methods to

Get & Print total number of Turkeys.

Get & Print total number of adult Turkeys.

Get & Print total number of not adult Turkeys.

Get & Print total number of paired Turkeys

Get & Print total number of not paired Turkeys

The necessary variables and changes needed to make the above static methods work as required.

Additional methods should be implemented as needed if they make the above methods easier to code.

//package Lab08;

import java.util.Random;

import Lab08.Lab_8.Turkey;

public class Turkey {

private final int MAX_AGE = 100; // days

private final int PREGNANCY_DURATION = 7; // a turkey is born 7 days

                                               // after being pregnant

private final int ADULT_AGE = 20; // Age at which a turkey is considered an

                                       // adult

private int gender; // 1 or 2

private int id; // unique ID

private int age; // in days, 0 initially

private int pairedWithID; // 0 if not paired.

private int daysPregnant; // 0 initially

private boolean isPregnant; // initially false

private boolean isAdult; // initially false

static Random gen = new Random(System.currentTimeMillis());

// static variables add them here

private static int nextID = 1; // This holds the value of the next free ID

// Constructor that creates a turkey of a random gender.

// add code here

public Turkey() {

       // This is the constructor for a Turkey. When A turkey is

       // born/constructed It will get a random gender, 1 or 2.

       // Use the random number generator above.

       // The other variable we will set

       // to 0 Initially.

   }

public static Turkey birth(Turkey t1) {

       // Turkey 1 should no longer be pregnant add code to change to not

       // pregnant

       return new Turkey();

   }

public boolean isAdult() {

       // returns true if age is over ADULT_AGE

       return false;

   }

public boolean isPregnant() {

       return false;

   }

public boolean istimeToDie() {

       // returns true if age is > max_Age

       return false;

   }

public int getID() {

       // returns the value of the ID of the current Turkey

       return 0;

   }

public int getPairID() {

       // this method returns the pairID, the ID of the turkey this one is pair

       // with.

       return 0;

   }

public void unPair() {

       // This method removes the value for pairID and sets it to zero

   }

public void passTime() {

       // add one to each of the day values. This includes the age and

       // daysPregnant.

   }

public static void slaughter() {

       // increments a counter that keeps track of how many turkeys have been

       // killed.

   }

public boolean isPaired() {

       // return true if the turkey pairID is != 0;

       return false;

   }

public static void mate(Turkey t1, Turkey t2) {

       // Always set female to pregnant

       // Determine which of the two Turkey's is female and make pregnant and

       // set days pregnant to zero

   }

private void setDaysPregnant(int days) {

   }

public static void pairTurkeys(Turkey t1, Turkey t2) {

       // This method will set pair ID of each other

   }

private void setPairIDTo(int id) {

   }

public int getGender() {

       return 0;

   }

public boolean isPregnancyDue() {

       // This method will return true if the daysPregnant is greater than

       // PREGNANCYDURATION

       return false;

   }

public void setIsPregnant(boolean p) {

   }

// toString method that returns important information about a Turkey

}

//package Lab08;

import java.util.ArrayList;

import java.util.Random;

import Lab08.Lab_8.Turkey;

public class TurkeyFarm {

public static void main(String[] args) {

       ArrayList farm = new ArrayList();

       Random gen = new Random(System.currentTimeMillis());

      

       //Five turkeys may not always have both genders

       int startTurkeys = /* gen.nextInt(100) + */ 5;

       for (int i = 0; i < startTurkeys; i++) {

           farm.add(new Turkey());

       }

       int daysMax = 365;

       int days = 0;

       while (days <= daysMax) {// for each day

           System.out.println("Day:" + days);

           for (Turkey t1 : farm) {

               // check if adult{

               if (t1.isAdult()) {

                   // if paired & not pregnant call mate

                   if (!t1.isPregnant() && t1.isPaired()) {

                       for (Turkey t2 : farm) {

                           // find a turkey's pair and mate

                           // The turkey is only paired with the opposite

                           // gender

                           if (t1.getID() == t2.getPairID()) {

                               Turkey.mate(t1, t2);

                           }

                       }

                   }

                   // if not paired pair up with a free turkey of the opposite

                   // gender

                   if (!t1.isPaired())

                       for (Turkey t2 : farm) {// find a turkey to pair up with

                           if (!t2.isPaired() && t1.getGender() != t2.getGender())

                               Turkey.pairTurkeys(t1, t2);

                       }

               }

           }

           for (int j = farm.size() - 1; j >= 0; j--) {

               Turkey t1 = farm.get(j);

               // if pregnant give birth to new turkeys?

               if (t1.isPregnant() && t1.isPregnancyDue()) {

                   int x = gen.nextInt(3) + 1;

                   for (int i = 0; i <= x; i++)

                       farm.add(Turkey.birth(t1));

               }

           } // end check if adult

           for (int i = farm.size() - 1; i >= 0; i--) {

               Turkey t1 = farm.get(i);

               // is too old? Time to die

               if (t1.istimeToDie()) {

                   // Unpair other turkey if need be

                   for (Turkey t2 : farm) {

                       if (t2.getPairID() == t1.getID()) {

                           //We unpair both to keep the counts of those

                           //paired and unpaired accurate.

                           t1.unPair();

                           t2.unPair();

                       }

                   }

                   Turkey.slaughter();

                   farm.remove(t1);

               }

               // PassTime Method

               t1.passTime();

           } // end for

           // Status of the Farm. These should be static method calls

           // Get & Print total number of Turkeys;

           // Get & Print total number of adult Turkeys

           // Get & Print total number of notAdult Turkeys

           // Get & Print total number of Paired Turkeys

           // Get & Print total number of NotPaired Turkeys

                      

           days++;

       } // end While loop

       // Display all the IDs of the turkeys left

       // If the toString method is not overridden we memory addresses

       // not the details of our turkeys!

       for (Turkey t1 : farm){

           System.out.println(t1);

       }

   }

}

Explanation / Answer

//package Lab08;

import java.util.Random;

public class Turkey {

private final int MAX_AGE = 100; // days

private final int PREGNANCY_DURATION = 7; // a turkey is born 7 days

                                               // after being pregnant

private final int ADULT_AGE = 20; // Age at which a turkey is considered an

                                       // adult

private int gender; // 1 or 2

private int id; // unique ID

private int age; // in days, 0 initially

private int pairedWithID; // 0 if not paired.

private int daysPregnant; // 0 initially

private boolean isPregnant; // initially false

private boolean isAdult; // initially false

   static Random gen = new Random(System.currentTimeMillis());

// static variables add them here

private static int nextID = 1; // This holds the value of the next free ID

// Constructor that creates a turkey of a random gender.

// add code here

public static int slaughtered = 0;

   public Turkey() {

       // This is the constructor for a Turkey. When A turkey is

       // born/constructed It will get a random gender, 1 or 2.

       // Use the random number generator above.

       // The other variable we will set

       // to 0 Initially.

       gender = (gen.nextInt(2))+1;

       id=nextID;

       age=0;

       pairedWithID=0;

       daysPregnant=0;

       isPregnant=false;

       isAdult=false;

       nextID++;

   }

   public Turkey birth(Turkey t1) {

       // Turkey 1 should no longer be pregnant add code to change to not

       // pregnant

       isPregnant=false;

       return new Turkey();

   }

   public boolean isAdult() {

       // returns true if age is over ADULT_AGE

       if (age > ADULT_AGE) {

           return true;

       }

       return false;

   }

   public boolean isPregnant() {

       return isPregnant;

   }

   public boolean istimeToDie() {

       // returns true if age is > max_Age

       if (age > MAX_AGE) {

           return true;

       }

       return false;

   }

   public int getID() {

       // returns the value of the ID of the current Turkey

       return id;

   }

   public int getPairID() {

       // this method returns the pairID, the ID of the turkey this one is pair

       // with.

       return pairedWithID;

   }

   public void unPair() {

       // This method removes the value for pairID and sets it to zero

       pairedWithID=0;

   }

   public void passTime() {

       // add one to each of the day values. This includes the age and

       // daysPregnant.

       age+=1;

       if (isPregnant) {

           daysPregnant+=1;

       }

   }

   public static void slaughter() {

       // increments a counter that keeps track of how many turkeys have been

       // killed.

       slaughtered++;

   }

   public boolean isPaired() {

       // return true if the turkey pairID is != 0;

       return getPairID() != 0 ? true : false;

   }

   public static void mate(Turkey t1, Turkey t2) {

       // Always set female to pregnant

       // Determine which of the two Turkey's is female and make pregnant and

       // set days pregnant to zero

       if (t1.gender == 1) {

           t2.setIsPregnant(true);

           t2.setDaysPregnant(0);

       } else {

           t1.setIsPregnant(true);

           t1.setDaysPregnant(0);

       }

   }

   private void setDaysPregnant(int days) {

       daysPregnant=days;

   }

   public static void pairTurkeys(Turkey t1, Turkey t2) {

       // This method will set pair ID of each other

       t1.setPairIDTo(t2.getID());

       t2.setPairIDTo(t1.getID());

   }

   private void setPairIDTo(int id) {

       pairedWithID=id;

   }

   public int getGender() {

       return gender;

   }

   public boolean isPregnancyDue() {

       // This method will return true if the daysPregnant is greater than

       // PREGNANCYDURATION

       if (daysPregnant > PREGNANCY_DURATION) {

           return true;

       }

       return false;

   }

   public void setIsPregnant(boolean p) {

       isPregnant=true;

   }

// toString method that returns important information about a Turkey

   public String toString() {

       String str = "Turkey with id: "+id;

       str+=" is "+(gender==1 ? "male" : "female");

       str+=" and it is "+(isPregnant == true ? "" : "not ")+"pregnant";

      

       return str;

   }

}