It's not a secret anymore!
The moose likes Java in General and the fly likes Help with project Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

Help with project

Vivienne Ryan

Joined: Dec 12, 2013
Posts: 12

Hi there,
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.
Roger Sterling
Ranch Hand

Joined: Apr 06, 2012
Posts: 426

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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8945

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()).


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8945

Closing this thread, as your code (and the discussion) seems to have moved on.

I agree. Here's the link:
subject: Help with project
It's not a secret anymore!