import java.util.Random; public class Turkey { private final int MAX_AGE = 100;
ID: 3761267 • Letter: I
Question
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 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
String isAdult;
if (age >= ADULT_AGE )
isAdult = "This turkey has reached the max age";
else
return false;
}
public boolean isPregnant() {
return false;
}
public boolean istimeToDie() {
// returns true if age is > max_Age
if (age > MAX_AGE)
istimeToDie = "Turkey reached maximum age limit, might die soon";
else
return false;
}
public int getID() {
// returns the value of the ID of the current Turkey
class getID
{
int ID = 1;
getID()
{
ID ++;
}
}
getID getID = new getID();
System.out.println(getID.ID);
}
else;
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 set
Explanation / Answer
Below is method implementations. I have added few new methods and variables to simplfy the code logic
package eclipsePackage;
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
private static int slaughterCount=0;
private static int numberofAdultTurkeys=0;//Increment it if the turkey id adult using SetAdult method
private static int nonPaired=0;////Increment it if the turkey id adult using SetNonPairedCount method
private static int paired=0;////Increment it if the turkey id adult using SetPairedCount method
private static int nextID = 1;
public Turkey() {
//no Need to initialize other variable as they will get assigned to their default values once object get created
//If you have any specific requirement then you can assign the variable here.
gender=(int) ( Math.random() * 2 + 1);//This will generate a random number either 1 or 2
nextID+=1;
}
public static Turkey birth(Turkey t1) {
t1.isPregnant=false;
return new Turkey();
}
public boolean isAdult() {
// returns true if age is over ADULT_AGE
if (age >= ADULT_AGE )
return true;
else
return false;
}
public boolean isPregnant() {
if(isPregnant==true)
{
return true;
}
else
{
return false;
}
}
public boolean istimeToDie() {
// returns true if age is > max_Age
if (age > MAX_AGE)
return true;
else
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==true)
{
daysPregnant+=1;
}
}
public static void slaughter() {
slaughterCount+=1;
}
public boolean isPaired() {
// return true if the turkey pairID is != 0;
if(pairedWithID!=0)
{
return true;
}
else
{
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
if(t1.gender==2)//Considering 2 being female you can change it accordingly
{
t1.isPregnant=true;
t1.daysPregnant=0;
}
if(t2.gender==2)
{
t2.isPregnant=true;
t2.daysPregnant=0;
}
}
public static void pairTurkeys(Turkey t1, Turkey t2) {
t1.pairedWithID=t2.id;
t2.pairedWithID=t1.id;
}
public boolean isPregnancyDue() {
// This method will return true if the daysPregnant is greater than
// PREGNANCYDURATION
if(daysPregnant>PREGNANCY_DURATION)
{
return true;
}
else
{
return false;
}
}
public static void SetAdult()
{
numberofAdultTurkeys+=1;
}
public static void SetNonPairedCount() {
nonPaired+=1;
}
public static void SetPairedCount() {
paired+=1;
}
public String tostring()
{
String information;
//Here i am adding basic information about a turkey you can add more if you want.
information="Turkey Id= "+id+" "+"Paired With ID: "+pairedWithID+" "+"Pregnant Status"+isPregnant+" ";
return information;
}
public static void PrintTotalNoOfTukeys()
{
System.out.println("Total Number of TuKeys: "+ nextID);
}
public static void PrintTotalNoOfAdultTukeys()
{
System.out.println("Total Number of Adult Turkey: "+ numberofAdultTurkeys);
}
public static void PrintTotalNoOfNONAdultTukeys()
{
System.out.println("Total Number of Non Adult Turkey: "+ (nextID-numberofAdultTurkeys));
}
public static void PrintTotalNumberOfPairedTurkey()
{
System.out.println("Total Number of Paired Turkey: "+ paired);
}
public static void PrintTotalNumberOfNONPairedTurkey()
{
System.out.println("Total Number of NON Paired Turkey: "+ nonPaired);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.