Issues translating script from python to java

In my computer science class, I received an assignment that required me to write a small script to predict the rate of rabbit population each year with given parameters. Originally, I was supposed to write it in java, but because I am a noob in that language, I decided to first write it out in python. After doing so, the python script ended up working and yielding the correct results. However, when I attempted to write the script in java, the result is completely different compared to the python version. I spent countless hours looking over the java code, and I can't find out why this is happening.

This is the python version:

``````from time import sleep
##Summer to summer basis
##There are E female rabbits and E male rabbits in summer of year one
##Rabbits can't reproduce in the first year, so for year one E females and E males will remain the same
##Every winter, AI kills percentage P of all female and male rabbits equally
##If outcome is a decimal, round DOWN
##Every surviving female will give birth to R baby rabbits in the spring
##These rabbits will reach reproduction age by the next spring, if they survive the AI winter

def calcPopulation(E,P,R,Y):
if E > 1000 or P > 100 or R > 50 or Y > 10:
print("One or more values that you entered is too high")
return None
elif Y == 1:
return E*2

males = E
females = E
#^For first year
for i in range(Y-1): #males
males -= int((P*males)/100)

for i in range(Y-1):
females += int(-(P*females)/100)
for i in range(int(females)):
##            femalebaby = (R/2)
##            malebaby = (R/2)
females+= (R/2)
males+= (R/2)
return int(males+females)

def main():
print(calcPopulation(100,50,9,3))

if __name__=="__main__":
main()
``````

This is the java version, implemented with user input:
``````import java.util.*;

public class rabbit{

public static int get_input(){
return 0;

}

public static void print(String value){
System.out.print(value);
}

public static int calcPopulation(int E,int P,int R,int Y){
if (E>1000||P>100||R>50||Y>10){
print("One or more of the values you entered is too high");
return 0;
}
else if (Y==1){
return E*2;
}
int males=0;
int females=0;
males += (int) E;
females += (int) E;
for(int i=0;i<(Y-1);i++){
males -= (int) (P*males)/100;
}
for(int i=0;i<(Y-1);i++){
females -= (P*females)/100;
for(int x=0;x<(Y-1);x++){
females += (R/2);
males += (R/2);
}
}
return (int) (males+females);
}

static Scanner input = new Scanner(System.in);
public static void main(String[] args){
System.out.println("Enter E parameter");
int E = input.nextInt();
System.out.println("Enter P parameter");
int P = input.nextInt();
System.out.println("Enter R parameter");
int R = input.nextInt();
System.out.println("Enter Y parameter");
int Y = input.nextInt();
System.out.println(calcPopulation(E,P,R,Y));
//parameters in the python script are: 100,50,9,3
}

}
``````
The correct output from the python script with thew given parameters is: 1630, while the java version outputs 78. Why are they not both yielding 1630?

-Any help is greatly appreciated
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
In the Python script starting on line 23, the inner loop also uses the local index variable i, while the java version has a different variable x for counting (line 31). Since you said the Python version works correctly you should also iterate on i in the java version.
0
Author Commented:
In the python version, each ‘i’ is isolated in its own for loop, meaning that I could change the i in the second for loop and the python script will still work the same. I tried using two i variables in the java loops, but because one loop was in the other, I would get an error. I guess that’s one of the differences between the two languages. Correct me if I’m wrong. Is there anything else that could be wrong in my java program?
0
Commented:
The 'i' is not exactly isolated but I see that it iterates just fine. I just never tried using the same var name in a nested loop ^^.
Thats really a tough nut then. The only other possible difference I found is, that python returns a double value in lines 32 and 33, that are casted to int after they are summed (I'm assuming python 3.x.x here). In Java the division yields an integer directly, so the part after the comma is not added in in the java version.
0
Commented:
I suppose it's no longer important to you since this question is no longer active, but I have found the problem and it is indeed related to the return value of the division. I modified the java function and it returns the same as the python version:
``````public static int calcPopulation(int E, int P, int R, int Y){
if (E>1000 || P>100 || R>50 || Y>10){
print("One or more of the values you entered is too high");
return 0;
}
else if (Y==1){
return E*2;
}
double males = E;
double females = E;
for (int i=0; i<(Y-1); i++){
males -= (int) (P*males) / 100;
}
for (int i=0; i<(Y-1); i++){
females -= (int) (P*females) / 100;
int limit = (int) females;
for(int x = 0; x < limit; x++){
females += ((double) (R) / 2.0);
males += ((double) (R) / 2.0);
}
}
return (int) (males + females);
}
``````
I completely overlooked, that you had the wrong loop limit in line 31 of your  java code. Instead of the year you would need the number of females at that point. Also male and female need to be of type double and the division needs to be casted to double. This is really of no concern in Python, since it is dynamically typed. Also I added an extra variable for the counter, because Python creates an Iteratable using range, but java would check against the variable females in each iteration as its value changes. Maybe you've found the solution yourself, but if not you like thhis question beeing answered at last ^^.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thank you so much!! It’s been all this time, yet I still haven’t found the fix until now👍👍
0
Commented:
Since Peter Filip hasn't closed the question I took the freedom to do so
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.