java pseudocode Part 1 Loving your Amoeba Colony Your little
java pseudocode
Part 1: Loving your Amoeba Colony!
Your little sister has decided that she wants a pet. You love animals and want to further engender her love of animals, but, well, her last pet, goldie the goldfish, didn’t fare too well. So you went on the hunt for a different kind of pet that will fit the bill. Much to your joy, you have found the perfect pet for her… an amoeba colony! They are easy to take care of and don’t die off too quickly. Now all that you have to do is build a caretaker program so that she can easily take care of her amoebas and see how they thrive (or not!).
Here’s what you need for the amoeba colony (hint: these can be be passed/given to the class constructor if you want to challenge yourself):
Colony name – Every pet needs a name, right? So you’ll need to ask the user what the name of their colony is.
Caretaker name – You’ll need to ask who the colony’s mom/dad is.
Starting size – How many amoebas is the colony starting out with? This can vary, so you better ask the user about that also.
These are the things that can be done with the colony:
(hint: Do not assume that these must be methods. Think about how they will be used and then decide)
Feed – Like all pets, amoebas get hungry. In our case, though, only when they are going to breed. Fortunately, with the program you are building, you can ask the user how many days their colony should be fed and then check to make sure they have enough food to breed (they need 1 day of food for each time they breed).
Breed – Other than eating, amoebas don’t do much else but breed. You’ll want to be sure they have some entertainment, so you’ll need to ask your user if they want to breed their amoebas and, if so, how many times. For each time they successfully breed, the colony doubles in size. (hint: re-read about feeding)
Vitamins – Vitamins can help our amoebas stay healthy. Ask your user if they want to give their colony vitamins.
Sickness – Unfortunately, all pets sometimes get sick. When that happens, 10% of the amoebas die off. For every session:
If the colony is NOT given vitamins, it has a 25% chance that it may get sick.
If the colony IS given vitamins, it has a 20% chance that it may get sick.
(hint: you can use the random number generator from Assignment 1 to help you with this)
For this program, you’ll want to ask your user about any pertinent information up front. Do not worry about having any loops to ask them things like “Do you want to feed your colony again?” You will only ask them ONCE for the needed information and then tell them how their colony is doing.
For your output (nicely formatted in a JOptionPane), you will want to include:
Colony Name
Caretaker Name
Starting Size
How many times they were fed
Requested number of times to breed
How many times they successfully bred
Whether they got sick and how many died
Final number of amoebas in the colony
For Part 1, create a class structure and algorithms for your AmoebaColony class, and then do several iterations of tests (i.e., analyze it and step through to make sure that it is logically correct). Also write the pseudocode for your tester class (where your main will go). Put these in a Word or Open Office document. You’ll turn that document in with the program that you create in Part 2.
Solution
Pseudocode for Tester Class
Pseudocode for AmoebaColony Class
A. setBreeding() method
B. setVitamins() method
C. setSickness() method
D. SetDeath() method
AmoebaTester.java
package amoebacolony;
import javax.swing.JOptionPane;
public class AmoebaTester {
public static void main(String[] args){
AmoebaColony newColony = new AmoebaColony();
try{
newColony.setColonyProperties();
newColony.setActions();
newColony.setBreeding();
newColony.setVitamins();
newColony.setSickness();
newColony.setDeath();
newColony.finalOutput();
}catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, \"You entered incorrect or incomplete information!\", \"Error\", JOptionPane.ERROR_MESSAGE);
}
}
}
AmoebaColony.java
package amoebacolony;
import javax.swing.JOptionPane;
import java.util.Random;
public class AmoebaColony {
public String colonyName;
public String caretakerName;
public String sickness;
public int startAmoeba;
public int nextAmoeba;
public int daysFed;
public int amountBreed;
public double finalAmoeba;
public double amountDied;
public int yesnoVitamins;
public int breedSuccessNumber;
public boolean vitamins;
public boolean sick;
public boolean colonyname;
public void setColonyProperties(){
String welcomeMsg = (\"Hello and welcome to our program.\ This program allows you to create and take care of your own amoeba colony!\");
String title = (\"Welcome!\");
JOptionPane.showMessageDialog(null, welcomeMsg, title, JOptionPane.INFORMATION_MESSAGE);
colonyName = JOptionPane.showInputDialog(\"What would you like to name your colony?\");
caretakerName = JOptionPane.showInputDialog(\"What is your name, caretaker?\");
startAmoeba = Integer.parseInt(JOptionPane.showInputDialog(\"How many amoebas would you like to start with?\"));
}// end setColonyProperties()
public void setActions(){
JOptionPane.showMessageDialog(null, \"Congratulations, \" + caretakerName+ \", you\'ve created your amoeba colony, \" + colonyName + \", of \" + startAmoeba + \" amoebas!\",
colonyName, JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null, \"Now you can feed your colony, breed your colony, and give them vitamins to prevent sickness!\" ,
colonyName, JOptionPane.INFORMATION_MESSAGE);
daysFed = Integer.parseInt(JOptionPane.showInputDialog(null, \"How many days would you like to feed your colony?\"
+ \"\ Note: They need one day of food for each time they breed.\",
\"Feeding\", JOptionPane.INFORMATION_MESSAGE));
amountBreed = Integer.parseInt(JOptionPane.showInputDialog(null, \"How many times would you like to breed your colony?\"
+ \"\ Note: Each successful breed doubles your colony\'s size.\"
+ \"\ Every time they breed requires 1 day worth of food.\"
+ \"\ You have chosen to feed your colony \" + daysFed + \" time(s).\",
\"Breeding\", JOptionPane.INFORMATION_MESSAGE));
yesnoVitamins = JOptionPane.showConfirmDialog(null, \"Would you like to give your colony vitamins?\"
+ \"\ Note: Giving vitamins reduces the chance of your colony getting sick.\", \"Vitamins\", JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_OPTION);
//Above is asking user what they would like to do about feeding, breeding, and vitamins
}// end setActions()
public void setBreeding(){
if (daysFed >= amountBreed){
breedSuccessNumber = amountBreed;
}
else{
breedSuccessNumber = daysFed;
}
int previousAmoeba = startAmoeba;
for (int i = 0; i < breedSuccessNumber; i++){
nextAmoeba = previousAmoeba * 2;
previousAmoeba = nextAmoeba;
}
nextAmoeba = nextAmoeba;
}// end setBreeding()
public void setVitamins(){
if (yesnoVitamins == JOptionPane.YES_OPTION){
vitamins = true;
}else if(yesnoVitamins ==JOptionPane.NO_OPTION){
vitamins = false;
}
//decision to give vitamins
}// end setVitamins()
public void setSickness(){
Random r = new Random();
int x = 1 + r.nextInt(100);
if(vitamins == true){
if(x <= 20){
sick = true;
sickness = \"Yes\";
}else{
sick = false;
sickness = \"No\";
}
} else{
if(x <= 25){
sick = true;
sickness = \"Yes\";
}else{
sick = false;
sickness = \"No\";
}
}
}// end setSickness()
public void setDeath(){
if(sick == true){
finalAmoeba = Math.round(nextAmoeba * .90);
}else{
finalAmoeba = nextAmoeba;
}
amountDied = nextAmoeba - finalAmoeba;
}// end setDeath()
public void finalOutput(){
JOptionPane.showMessageDialog(null, \"Summary of your new Amoeba Colony!:\"
+ \"\ Colony Name: \" + colonyName
+ \"\ Caretaker Name: \" + caretakerName
+ \"\ Starting Size: \" + startAmoeba + \" amoeba(s).\"
+ \"\ How many times fed: \" + daysFed
+ \"\ Number of times to breed request: \" + amountBreed
+ \"\ Number of successful breeds: \" + breedSuccessNumber
+ \"\ Sickness?: \" + sickness
+ \"\ How many died?: \" + amountDied
+ \"\ Final number of amoebas in colony: \" + finalAmoeba);
}
}



