mardi 6 octobre 2015

Why doesnt the colonySize variable change?

In this program, there is a 20% chance the amoeba colony will get sick if it has vitamins, and 25% it will get sick if it does not. Does the getSick() method represent this correctly? Also, no matter how many times I run the program, the colony never gets sick, and none of them die. which I find very peculiar. What do you suggest I change? Thanks for your help.

 public class AmoebaColony {

    //instance variables
    private String name;
    private String caretakerName;
    private long colonySize;
    private boolean isVitamin; 
    private int successfulBreeds = 0; 
    private boolean sickness;

    //AmoebaColony constructor
    public AmoebaColony(String name, String caretakerName, long colonySize,
            boolean isVitamin) {
        this.name = name;
        this.caretakerName = caretakerName;
        this.colonySize = colonySize;
        this.isVitamin = isVitamin;
    }

    public void feed()
    {
        colonySize *= 2;
        successfulBreeds++;
    }

      public void getSick()
      {
          Random n = new Random(); 

          if (isVitamin)
          { 
              int c1 = n.nextInt(19);
              if (c1 == 0)
              { sickness = true; }
              else 
              { sickness = false; }
          }
          else if (isVitamin == false)
          {
              int c2 = n.nextInt(24);
              if (c2 == 0)
              { sickness = true; }
              else 
              { sickness = false; }
          }
      }  

      public String isSick()
      {
          if (sickness == true)
          {
              return "did";
          }
          else
          {
              return " did not ";
          }
      }

      public void die() {

          if (sickness == true)
          {
              colonySize = colonySize - (colonySize/10);
          }
          else if (sickness == false)
              colonySize = colonySize;

      }

       //returns how many times it successfully bred
       public int getSuccess()
       {
         return successfulBreeds;
       }

        //returns size of colony
       public long getSize()
       {
         return colonySize;
       }

}


    public class AmoebaColonyTester {

    public static void main(String[] args) {

        //Get variables
        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?");
        long colonySize = Integer.parseInt(JOptionPane.showInputDialog(null, "Welcome, " + caretakerName + "! 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 feedCounter = 0;

        //create colony object
        AmoebaColony amoeba = new AmoebaColony(name, caretakerName, colonySize, isVitamin);

        //feed the colony so it can breed
        for(int x = 1; x <= feedTimes; x++)
        {
            if (x <= breedTimes)
            { 
              amoeba.feed(); 
              feedCounter++;
            }

        }
        //get maximum colony size
        long finalColony = amoeba.getSize();
;
        //amoeba get sick :/
        amoeba.getSick();   
        amoeba.die();   
        System.out.println(amoeba.getSize());

        //display info on the screen
        JOptionPane.showMessageDialog(null, "The colony name is " + name + "\nThe caretaker name is " + caretakerName +
                                     "\nThe starting colony size was " + colonySize + "\nThey were fed " + feedCounter
                                     + " times \nRequested number of times to breed: " + breedTimes
                                     + " \nThey successfully bred " + amoeba.getSuccess() + " times\nThe colony " + amoeba.isSick() +
                                     " get sick and "+ (finalColony - amoeba.getSize()) + " died \n"
                                     + " The final size of the colony is " + amoeba.getSize());


    }

}




Aucun commentaire:

Enregistrer un commentaire