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;
}
}
Sample input: Colony name : bobo Caretaker name : Peter colony size : 500 feed times: 4 breed times: 7 // in this case it will breed successfully 4 times isVitamin: No vitamins (false)
I would recommend just running this through some simple debugging. Follow these steps and it should narrow down where your problem is.
From what I can see of the code, I do not see a reason it should not properly update the success.
Edit I see your problem. You are inputting the values specified, and this is how the program interprets it. Your breed function uses the following values, you enter breed=7, feed=4. I put these values into the function below...
if(7<= 4){ // this is obviously false, so it uses the else
colonySize *= 2;
feedTimes--;
breedTimes--;
return 1;
} else{
//Since this is run, and both are decremented togeather, you will always be in the Else.
feedTimes--;
breedTimes--;
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.