if / else if inside getter/setter Java -
i started education software engineering learning basics of java... have create simple text-based airport game , ran little trouble.
i tried google , sof search function , found many "solutions" however; none seem fix problem. forgive me "noob" qeustion :p
in game have several airports generates random amount of passengers each round, minimum of 100 , maximum of 999. used following code... works , doesnt give me errors still random results below 100. doing wrong? said tried many different ways seems neglect if / if else conditions...
int paxgenerator; public int getpaxgenerator() { return this.paxgenerator = (int) (math.random() * 1000); } public void setpaxgenerator(int paxgenerator) { if (paxgenerator<100) { this.paxgenerator = 100; } else if (paxgenerator > 999) { this.paxgenerator = 999; } }
edit:
return this.paxgenerator = (int) (math.random() * 900) + 100;
indeed fix problem, thx @user43219 however, sake of learning; can give me example of how should correctly implement if else statement? because want create 12 new objects, have generate different random number. if place random code outside getter/setters gives me same number everytime... , if else statement isnt allowed outside getter/setters... bear in mind im beginner things arent obvious me :p thx in advance!
you not calling setter. line:
return this.paxgenerator = (int) (math.random() * 1000);
does not implicitly call setpaxgenerator()
- bypasses setter , sets property value directly.
Comments
Post a Comment