Question

Basically, there is a group of 20 sheep. After the group has grown to a population of 80 sheep, the group does not need to be supervised anymore. The number of sheep, N, each year, t, is found with :

N = 220/(1 + 10(0.83)^t)

This program tries to find out how many years the sheep have to be supervised and writes out the value of N for t starting at zero and going up to 25.

This is my code so far...it doesn't seem to work and I know there is something to do with the part about multiplying with the power. I'm trying to use a variable "power" that is multiplied by 0.83 in each iteration of the loop. Any help is appreciated, thank you.

   public static void main(String[] args) {

    System.out.println("A breeding group of 20 bighorn sheep is released in a protected area in Colorado.");
    System.out.println("After the group has reached a size of 80 sheep, the group does not need to be supervised anymore.");
    System.out.println("This program calculates how many years the sheep have to be supervised.");
    int number = 20;
    int power = 1;
    for(int years = 0; number < 80; power*= 0.83) {
        number = 220 / (1 + 10 * power);           
        System.out.println("After " + years + " years, the number of sheep is: " + number);
        years++;
    }
}
 }
Was it helpful?

Solution

change your data types on number and power from int to double. I tried it and it runs correctly. You also might want to modify your for loop to run while years < 25 rather than number < 80. And make number a local variable inside the loop for cleanliness.

public static void main(String[] args) {
    System.out.println("A breeding group of 20 bighorn sheep is released in a protected area in Colorado.");
    System.out.println("After the group has reached a size of 80 sheep, the group does not need to be supervised anymore.");
    System.out.println("This program calculates how many years the sheep have to be supervised.");
    double power = 1;
    boolean foundFirstOverEighty = false;
    for (int years = 0; years < 25; years++) {
        double number = 220 / (1 + 10 * power);
        System.out.println("After " + years + " years, the number of sheep is: " + number);

        if (!foundFirstOverEighty && number >= 80) {
            System.out.println("First time number of sheep exceeded eighty. " + years + " years. number of sheep is: " + number);
            foundFirstOverEighty = true;
        }

        power *= 0.83;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top