Win a copy of Learn Spring Security (video course) this week in the Spring forum!
    Bookmark Topic Watch Topic
  • New Topic

Help with project

 
Vivienne Ryan
Greenhorn
Posts: 12
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
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()).

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
Closing this thread, as your code (and the discussion) seems to have moved on.

Winston
 
    Bookmark Topic Watch Topic
  • New Topic