How does combining boolean and non-boolean if statements in java work? Is there a particular way the conditional statements need to be written?
For example, the code below gives me different results every time.
Beetles are initialized as randomly male or female and at age 0.
public class BeetleAgent {
private Context<Object> context;
private Geography<Object> geography;
public boolean isFemale;
public int age;
public BeetleAgent(Context<Object> context, Geography<Object> geography, boolean isFemale, int age) {
this.context = context;
this.geography = geography;
this.isFemale = isFemale;
this.age= age;
}
public boolean isFemale() {
return isFemale;
}
public void setFemale(boolean isFemale) {
this.isFemale = isFemale;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//age in days
@ScheduledMethod(start = 1, interval = 1)
public void trackAge () {
this.setAge(getAge() + 1);
System.out.println("Beetle is now " + this.getAge());
}
// adults mate after 1 week
@ScheduledMethod(start = 1, interval = 1)
public void checkMate() {
int age = this.getAge();
if ((age == 7) && (this.isFemale() == true)) {
System.out.println("Both are true, beetle can MATE.");
mate();
}
}
@ScheduledMethod(start = 1, interval = 1)
public void checkMaleDeath() {
int age = this.getAge();
if ((this.isFemale() != true) && (age == 13)) {
System.out.println("Both are true, male beetle can DIE.");
maleDeath();
}
}
@ScheduledMethod(start = 1, interval = 1)
public void checkFemaleDeath() {
int age = this.getAge();
if ((this.isFemale() == true) && (age == 25)) {
System.out.println("Both are true, female beetle can DIE.");
femaleDeath();
}
}
public void mate() {
//mate code
}
public void femaleDeath() {
//death code
}
public void maleDeath() {
//death code
}
}
I have also tried for example below... among other combinations.
@ScheduledMethod(start = 1, interval = 1)
public void checkFemaleDeath() {
if ((this.isFemale == true) && (this.age == 25)) {
System.out.println("Both are true, female beetle can DIE.");
femaleDeath();
}
}
@ScheduledMethod(start = 1, interval = 1)
public void checkFemaleDeath() {
if (this.age == 25 && this.isFemale) {
System.out.println("Both are true, female beetle can DIE.");
femaleDeath();
}
}
I cant imagine java doesn't allow for mixing...
Thanks in advance for insight.
Aucun commentaire:
Enregistrer un commentaire