Please, need help! For some rreason is just pritning the days 7 8 //package Lab0
ID: 3762999 • Letter: P
Question
Please, need help! For some rreason is just pritning the days
7
8 //package Lab08;
9
10 import java.util.Random;
11
12
13
14 public class Turkey {
15 private final int MAX_AGE = 100; // days
16 private final int PREGNANCY_DURATION = 7; // a turkey is born 7 days
17 // after being pregnant
18 private final int ADULT_AGE = 20; // Age at which a turkey is considered an
19 // adult
20 private int gender; // 1 or 2
21 private int id; // unique ID
22 private int age; // in days, 0 initially
23 private int pairedWithID; // 0 if not paired.
24 private int daysPregnant; // 0 initially
25 private boolean isPregnant; // initially false
26 private boolean isAdult; // initially false
27
28 static Random gen = new Random(System.currentTimeMillis());
29
30 // static variables add them here
31 private static int nextID = 1; // This holds the value of the next free ID
32 private static int slaughterCount=0;
33 private static int numberofAdultTurkeys=0;//Increment it if the turkey id adult using SetAdult method
34 private static int nonPaired=0;////Increment it if the turkey id adult using SetNonPairedCount method
35 private static int paired=0;////Increment it if the turkey id adult using SetPairedCount method
36
37 // Constructor that creates a turkey of a random gender.
38 // add code here
39 public Turkey()
40 {
41 // This is the constructor for a Turkey. When A turkey is
42 // born/constructed It will get a random gender, 1 or 2.
43 // Use the random number generator above.
44 // The other variable we will set
45 // to 0 Initially.
46 gender=(int) ( Math.random() * 2 + 1);
47 nextID+=1;
48 }
49
50 public static Turkey birth(Turkey t1)
51 {
52 // Turkey 1 should no longer be pregnant add code to change to not
53 // pregnant
54 t1.isPregnant=false;
55 return new Turkey();
56 }
57
58 public boolean isAdult()
59 {
60 // returns true if age is over ADULT_AGE
61 if (age >= ADULT_AGE )
62 {
63 return true;
64 }
65 else
66 {
67 return false;
68 }
69 }
70
71 public boolean isPregnant()
72 {
73 if(isPregnant==true)
74 {
75 return true;
76 }
77 else
78 {
79 return false;
80 }
81 }
82
83 public boolean istimeToDie()
84 {
85 // returns true if age is > max_Age
86 if (age > MAX_AGE)
87 {
88 return true;
89 }
90 else
91 {
92 return false;
93 }
94 }
95
96 public int getID()
97 {
98 // returns the value of the ID of the current Turkey
99 return id;
100 }
101
102 public int getPairID()
103 {
104 // this method returns the pairID, the ID of the turkey this one is pair
105 // with.
106 return pairedWithID;
107 }
108
109 public void unPair()
110 {
111 // This method removes the value for pairID and sets it to zero
112 pairedWithID=0;
113 }
114
115 public void passTime()
116 {
117 // add one to each of the day values. This includes the age and
118 // daysPregnant.
119 age+=1;
120 if(isPregnant==true)
121 {
122 daysPregnant+=1;
123 }
124 }
125
126 public static void slaughter()
127 {
128 // increments a counter that keeps track of how many turkeys have been
129 // killed.
130 slaughterCount+=1;
131 }
132
133 public boolean isPaired()
134 {
135 // return true if the turkey pairID is != 0;
136 if(pairedWithID!=0)
137 {
138 return true;
139 }
140 else
141 {
142 return false;
143 }
144 }
145
146 public static void mate(Turkey t1, Turkey t2)
147 {
148 // Always set female to pregnant
149 // Determine which of the two Turkey's is female and make pregnant and
150 // set days pregnant to zero
151 if(t1.gender==2)//Considering 2 being female you can change it accordingly
152 {
153 t1.isPregnant=true;
154 t1.daysPregnant=0;
155 }
156 if(t2.gender==2)
157 {
158 t2.isPregnant=true;
159 t2.daysPregnant=0;
160 }
161 }
162
163 private void setDaysPregnant(int days)
164 {
165
166 }
167
168 public static void pairTurkeys(Turkey t1, Turkey t2)
169 {
170 // This method will set pair ID of each other
171 t1.pairedWithID=t2.id;
172 t2.pairedWithID=t1.id;
173 }
174
175 private void setPairIDTo(int id)
176 {
177
178 }
179
180 public int getGender()
181 {
182 return 0;
183 }
184
185 public boolean isPregnancyDue()
186 {
187 // This method will return true if the daysPregnant is greater than
188 // PREGNANCYDURATION
189 if(daysPregnant>PREGNANCY_DURATION)
190 {
191 return true;
192 }
193 else
194 {
195 return false;
196 }
197 }
198
199 public void setIsPregnant(boolean p)
200 {
201
202 }
203
204 // toString method that returns important information about a Turkey
205 public String tostring()
206 {
207 String information;
208 information="Turkey Id= "+id+" "+"Paired With ID: "+pairedWithID+" "+"Pregnant Status"+isPregnant+" ";
209 return information;
210 }
211
212 public static void PrintTotalNoOfTukeys()
213 {
214 System.out.println("Total Number of TuKeys: "+ nextID);
215 }
216
217 public static void PrintTotalNoOfAdultTukeys()
218 {
219 System.out.println("Total Number of Adult Turkey: "+ numberofAdultTurkeys);
220 }
221
222 public static void PrintTotalNoOfNONAdultTukeys()
223 {
224 System.out.println("Total Number of Non Adult Turkey: "+ (nextID-numberofAdultTurkeys));
225 }
226
227 public static void PrintTotalNumberOfPairedTurkey()
228 {
229 System.out.println("Total Number of Paired Turkey: "+ paired);
230 }
231
232 public static void PrintTotalNumberOfNONPairedTurkey()
233 {
234 System.out.println("Total Number of NON Paired Turkey: "+ nonPaired);
235 }
236 }
237This is turkeyfarm
1 //package Lab08;
2
3 import java.util.ArrayList;
4 import java.util.Random;
5
6
7
8 public class TurkeyFarm
9 {
10
11 public static void main(String[] args)
12 {
13 ArrayList<Turkey> farm = new ArrayList<Turkey>();
14 Random gen = new Random(System.currentTimeMillis());
15
16 //Five turkeys may not always have both genders
17 int startTurkeys = gen.nextInt(100) + 5;
18 for (int i = 0; i < startTurkeys; i++)
19 {
20 farm.add(new Turkey());
21 }
22
23 int daysMax = 365;
24 int days = 0;
25 while (days <= daysMax) {// for each day
26 System.out.println("Day:" + days);
27 for (Turkey t1 : farm)
28 {
29
30 // check if adult{
31 if (t1.isAdult())
32 {
33 // if paired & not pregnant call mate
34 if (!t1.isPregnant() && t1.isPaired())
35 {
36 for (Turkey t2 : farm)
37 {
38 // find a turkey's pair and mate
39 // The turkey is only paired with the opposite
40 // gender
41 if (t1.getID() == t2.getPairID())
42 {
43 Turkey.mate(t1, t2);
44 }
45 }
46 }
47 // if not paired pair up with a free turkey of the opposite
48 // gender
49 if (!t1.isPaired())
50 for (Turkey t2 : farm)
51 {// find a turkey to pair up with
52 if (!t2.isPaired() && t1.getGender() != t2.getGender())
53 Turkey.pairTurkeys(t1, t2);
54 }
55 }
56 }
57 for (int j = farm.size() - 1; j >= 0; j--)
58 {
59 Turkey t1 = farm.get(j);
60 // if pregnant give birth to new turkeys?
61 if (t1.isPregnant() && t1.isPregnancyDue())
62 {
63 int x = gen.nextInt(3) + 1;
64 for (int i = 0; i <= x; i++)
65 farm.add(Turkey.birth(t1));
66 }
67
68 } // end check if adult
69
70 for (int i = farm.size() - 1; i >= 0; i--)
71 {
72 Turkey t1 = farm.get(i);
73 // is too old? Time to die
74 if (t1.istimeToDie())
75 {
76 // Unpair other turkey if need be
77 for (Turkey t2 : farm)
78 {
79 if (t2.getPairID() == t1.getID())
80 {
81 //We unpair both to keep the counts of those
82 //paired and unpaired accurate.
83 t1.unPair();
84 t2.unPair();
85 }
86 }
87 Turkey.slaughter();
88 farm.remove(t1);
89 }
90
91 // PassTime Method
92 t1.passTime();
93
94 } // end for
95
96 // Status of the Farm. These should be static method calls
97 // Get & Print total number of Turkeys;
98 // Get & Print total number of adult Turkeys
99 // Get & Print total number of notAdult Turkeys
100 // Get & Print total number of Paired Turkeys
101 // Get & Print total number of NotPaired Turkeys
102
103 days++;
104 } // end While loop
105
106 // Display all the IDs of the turkeys left
107 // If the toString method is not overridden we memory addresses
108 // not the details of our turkeys!
109 for (Turkey t1 : farm)
110 {
111 System.out.println(t1);
112 }
113
114 }
115 }
Explanation / Answer
//CHEGG EXPERT ANSWER
package Lab08;
import java.util.ArrayList;
import java.util.Random;
class Turkey {
public final int MAX_AGE = 100; // days
public final int PREGNANCY_DURATION = 7; // a turkey is born 7 days
// after being pregnant
public final int ADULT_AGE = 20; // Age at which a turkey is considered an
// adult
public int gender; // 1 or 2
public int id; // unique ID
public int age; // in days, 0 initially
public int pairedWithID; // 0 if not paired.
public int daysPregnant; // 0 initially
public boolean isPregnant; // initially false
public boolean isAdult; // initially false
static Random gen = new Random(System.currentTimeMillis());
// static variables add them here
public static int nextID = 1; // This holds the value of the next free ID
public static int slaughterCount = 0;
public static int numberofAdultTurkeys = 0;//Increment it if the turkey id adult using SetAdult method
public static int nonPaired = 0;////Increment it if the turkey id adult using SetNonPairedCount method
public static int paired = 0;////Increment it if the turkey id adult using SetPairedCount method
// 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.
gender = (int) (Math.random() * 2 + 1);
nextID += 1;
}
public int SetAdult() {
if (ADULT_AGE >= 20) {
numberofAdultTurkeys++;
}
return numberofAdultTurkeys;
}
public int SetNonPairedCount() {
if (ADULT_AGE >= 20) {
nonPaired++;
}
return nonPaired;
}
public int SetPairedCount() {
if (ADULT_AGE >= 20) {
paired++;
}
return paired;
}
public static Turkey birth(Turkey t1) {
// Turkey 1 should no longer be pregnant add code to change to not
// pregnant
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 id;
}
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() {
// increments a counter that keeps track of how many turkeys have been
// killed.
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 void setDaysPregnant(int days) {
this.daysPregnant = days;
}
public static void pairTurkeys(Turkey t1, Turkey t2) {
// This method will set pair ID of each other
t1.pairedWithID = t2.id;
t2.pairedWithID = t1.id;
}
public void setPairIDTo(int id) {
this.pairedWithID = paired;
}
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;
} else {
return false;
}
}
public void setIsPregnant(boolean p) {
}
// toString method that returns important information about a Turkey
public String tostring() {
String information;
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);
}
}
public class TurkeyFarm {
public static void main(String[] args) {
ArrayList<Turkey> farm = new ArrayList<Turkey>();
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;
Turkey t = new Turkey();
Turkey t1 = new Turkey();
Turkey t2 = new Turkey();
Turkey.pairTurkeys(t1, t2);
Turkey.mate(t1, t2);
t.id = 100;
t.age = 34;
t.daysPregnant = 2;
t.gender = 2;
//Turkey.nextID=101;
Turkey.nonPaired = 200;
Turkey.nextID = 300;
Turkey.numberofAdultTurkeys = 233;
Turkey.paired = 234;
Turkey.slaughterCount = 3;
Turkey.numberofAdultTurkeys = 234;
t.pairedWithID = 210;
Turkey.slaughterCount = 34;
t.SetAdult();
t.SetNonPairedCount();
t.SetPairedCount();
t.setDaysPregnant(270);
t.setIsPregnant(true);
Turkey.slaughter();
System.out.println("gender is" + t.getGender());
System.out.println("ID is" + t.getID());
System.out.println("pair id is" + t.getPairID());
System.out.println("Is Adult " + t.isAdult());
System.out.println("is paired " + t.isPaired());
System.out.println("is pregnency due" + t.isPregnancyDue());
System.out.println("is pregnent " + t.isPregnant());
t.passTime();
Turkey.slaughter();
String tostring = t.tostring();
System.out.println(tostring);
t.unPair();
Turkey.birth(t);
Turkey.PrintTotalNoOfAdultTukeys();
Turkey.PrintTotalNoOfNONAdultTukeys();
Turkey.PrintTotalNumberOfNONPairedTurkey();
Turkey.PrintTotalNumberOfPairedTurkey();
// 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
// If the toString method is not overridden we memory addresses
// not the details of our turkeys!
for (Turkey t1 : farm) {
System.out.println(t1);
}
}
}
sampleoutput
sending sample out because more than 65000 characters cannot be uploaded
Day:0
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:1
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:2
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:3
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:4
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:5
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:6
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:7
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:8
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:9
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:10
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:11
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:12
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:13
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:14
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235
Day:15
gender is2
ID is100
pair id is100
Is Adult true
is paired true
is pregnency duetrue
is pregnent false
Turkey Id= 100
Paired With ID: 210
Pregnant Statusfalse
Total Number of Adult Turkey: 235
Total Number of Non Adult Turkey: 66
Total Number of NON Paired Turkey: 201
Total Number of Paired Turkey: 235