Reputation: 182
I could only find a c# link for this and couldn't understand it so here goes..
I have a Player and a Target class in my program, and I want to be able to make like this:
public Player(int difficulty)
{
if(difficulty == 1)
health = hArray[0];
else
health = hArray[1];
}
where hArray is my array of possible health values. This works, but I also deal with playerHealth inside of my Target class just for ease of use because I'm new to java sort of and didn't know how else to do it, so that has something like this:
private int playerHealth;
public int getPlayerHealth()
{
return playerHealth;
}
public void setPlayerHealth(int playerHealth){
this.playerHealth = playerHealth;
}
But I don't know how to call this anymore because originally I had it set for Player to always start with 100 health. I changed it because I need to set the option of health value in Player as a requirement and now when I call this in main (psuedo-ish):
Your starting health: " + tgt.getPlayerHealth()
It just says 0 because the health was defined in Player. So I know why it isn't working but I'm not sure how to fix it, or if I can fix it with my current layout.
Here are my declarations/instanstiations (is this a word?):
Target tgt = new Target(dif);
Player p1 = new Player(dif);
Based off of:
System.out.println("\nEnter a difficulty 1-2 (entering an integer "
+ "other than 1 or 2 will default to 2):");
dif = scn.nextInt();
any help is appreciated
Upvotes: 1
Views: 415
Reputation: 38195
Given that you properly modeled your two classes (which I doubt a bit): if your Target
needs to know about the player health, it should have a reference to a Player
instance and not hold a copy of one of the private members of Player
. Then you'll access the player health via that instance:
target.getPlayer().getHealth();
target.getPlayer().setHealth(50);
But once a Target
has a reference to one Player
, the names of the classes (if not the design in its entirety) seem quite inadequate.
Upvotes: 2
Reputation: 5496
After knowing that Target
is suppose to be an entity of some kind that fights your Player
and makes them lose their health, hence the need for target to know about player health, then I think you would want to have an operation belonging to Player
that can fight Target
s (just pass Target
to Player
, or the other way around). Something along the lines of the following would most likely work:
public interface Entity
{
public int getStrength();
public int damageThatCanBeDoneToAnotherEntity(Entity someOtherEntity);
public int getHealth();
public void fightAnotherEntity(Entity someOtherEntity);
}
public class WeakMonster implements Entity...
public class StrongMonster implements Entity...
public class Player implements Entity...
This is your homework assignment so I'll let you fill in the gaps.
Upvotes: 1
Reputation: 35011
rather than having an array like you do with the hArray, you should create a function in Player like
public static int getPlayerInitialHealth(int difficulty)
then you can use that function in both of your classes (import into the second class)
Upvotes: 0