When the breed method is called and there is food available, the size of the colony doubles. The user is asked how many times they want to feed the colony and how many times they want the colony to breed. I need to output the amount of times that it successfully bred. I created an integer called success to keep track of the times it successfully bred, but (not surprisingly for me) does not work and returns 0. How can I fix this problem? Thanks.
AmoebaColony tester class
import javax.swing.JOptionPane;
public class AmoebaColonyTester {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog(null, "What will be the name of the colony?");
String caretakerName = JOptionPane.showInputDialog(null, "What is the name of the caretaker of the colony?");
int colonySize = Integer.parseInt(JOptionPane.showInputDialog(null, "What is the starting size of the colony?"));
int feedTimes = Integer.parseInt(JOptionPane.showInputDialog(null, "How many times do you want to feed the colony?"));
int breedTimes = Integer.parseInt(JOptionPane.showInputDialog(null, "How many times do you want your colony to breed?"));
int vitamin = JOptionPane.showConfirmDialog(null,"Do you want to give vitamins to your colony?", "Please select",
JOptionPane.YES_NO_OPTION);
boolean isVitamin;
if (vitamin == 1)
isVitamin = true;
else
isVitamin = false;
int success = 0;
AmoebaColony amoeba = new AmoebaColony(name, caretakerName, colonySize, feedTimes,
breedTimes, isVitamin);
for(int x = 1; x <= breedTimes; x++)
{
success += amoeba.breed();
}
amoeba.howManyDead();
System.out.println("the size of the colony is " + amoeba.getSize());
JOptionPane.showMessageDialog(null, "The colony name is " + name + "\nThe caretaker name is " + caretakerName +
"\nThe starting colony size was " + colonySize + "\nThey were fed " + feedTimes
+ " times \nRequested number of times to breed: " + breedTimes
+ "\nThey successfully bred " + success + "times \nThe final size of the"
+ " colony is " + amoeba.getSize());
}
}
AmoebaColony
public class AmoebaColony {
private String name;
private String caretakerName;
private int colonySize;
private int feedTimes;
private int breedTimes;
boolean isVitamin;
int successfulBreeds;
public AmoebaColony(String name, String caretakerName, int colonySize,
int feedTimes, int breedTimes, boolean isVitamin) {
this.name = name;
this.caretakerName = caretakerName;
this.colonySize = colonySize;
this.feedTimes = feedTimes;
this.breedTimes = breedTimes;
this.isVitamin = isVitamin;
}
public int getSize()
{
return colonySize;
}
public int breed()
{
if(breedTimes <= feedTimes){
colonySize *= 2;
feedTimes--;
breedTimes--;
return 1;
}
else{
feedTimes--;
breedTimes--;
return 0;
}
}
public int howManyDead()
{
Random random = new Random();
int pop = colonySize;
if (isVitamin)
{
int c1 = random.nextInt(4);
if (c1 == 1)
colonySize = colonySize - (colonySize/10);
else
colonySize = colonySize;
}
else if (isVitamin == false)
{
int c2 = 1 + random.nextInt(3);
if (c2 == 1)
colonySize = colonySize - (colonySize/10);
else
colonySize = colonySize;
}
return pop - colonySize;
}
}
Aucun commentaire:
Enregistrer un commentaire