I am currently in the process of completing a project for class. My partner and I are nearly finished it but we need to sort out one or 2 things.
This is my code. What I am having trouble with is: In the attack methods I want to pass in the opponents hp, deal damage and return the adjusted hp. However the hp is not changing. I originally passed the hp in as an int, but remembered the int is a primitive and a copy is only changed. So I changed to int to an Integer, however the same problem is arising. I am getting more and more stressed about this part as my deadline is due this friday and it is the only thing preventing us from handing up a complete project.
Any ideas as to where I am going wrong?
EDIT: Ignore the DiceRoller class, that serves no purpose at the moment.
Your attack functions should not need to have an argument, as they can reference the class's Integer variable directly. So for example, remove the vHp argument from these functions and have the logic in the functions reference hHp directly.
Wrapper objects such as Integer are immutable; the value of an Integer cannot be changed.
You should use int instead of Integer unless you have a very good reason for it (for example, when you want to store them in a collection).
Your attack1() etc. methods are returning a value, but you are not doing anything with the return value. You should assign the return value to a variable. For example:
It would be better if you would use more descriptive names instead of "hHp" and "vHp". Why not use "healthPoints"?
Note that Java uses pass-by-value when methods are called. The argument variable is a copy of the variable you passed in. If you modify the argument variable inside the method, then the original variable is not changed.
Vivienne Ryan wrote:Any ideas as to where I am going wrong?
Well, other than the good advice you've been given, I notice that you're not using your DiceRoller class; and when you do, I suspect you'll find that its roll() method doesn't return what you expect. I'll leave you to work out why (hint: have a good read of the docs for Math.random()).
Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here