2021-05-30

Is there a reason for why I am getting the output as 0, 0, 0, 0?

I have the following code, which is suppose to print out roach values after the respective functions have been executed (roach.breed & roach.spray). However, I am getting values of zero.

code for constructor:

public class Roach {
    private int roach;
    /**@param roaches variables for number of roaches

     */
    public void RoachPopulation(int roaches) {
        roach = roaches;

    }

    /** The population of roach doubles

     */
    public void breed() {
        roach = roach * 2;
    }
    /** The population of the roach decreases by a percentage


     */
    public void spray(double percent) {
        roach = roach -(int)(roach * (percent/100));
    }
    /**@return Gets the population of the roach


     */
    public int getRoaches() {

        return roach;
    }

}

The constructor:

public class HelloWorld {
    public static void main(String[] args) {
        Roach roach = new Roach();
        roach.breed();
        roach.spray(10);;
        System.out.println(roach.getRoaches());
        roach.breed();
        roach.spray(10);;
        System.out.println(roach.getRoaches());
        roach.breed();
        roach.spray(10);;
        System.out.println(roach.getRoaches());
        roach.breed();
        roach.spray(10);;
        System.out.println(roach.getRoaches());


    }
}

Output:

0
0
0
0


from Recent Questions - Stack Overflow https://ift.tt/3fynIS8
https://ift.tt/eA8V8J

No comments:

Post a Comment