Note that there are some explanatory texts on larger screens.

plurals
  1. POExternal Method call help
    primarykey
    data
    text
    <p>So what im trying to do is to call an external method upon an object, its a bit trickier than I expected and am having problems properly implementing it. The method is:</p> <p>attack(Player victim)</p> <p>The method needs to call a hit() method on an object; then if the hit() method was successful (test this through a boolean?):</p> <ul> <li><p>use an if statement to call a damage() method upon the object to determine the damage</p></li> <li><p>call takeDamage() upon (PlayerVictim) to inflict the damage.</p></li> </ul> <p>Here's the player class that ive coded so far; the attack() method is at the bottom. <strong>My main question is how to use an external method(s) damage() on the currentWeapon and takeDamage() on Player Victim</strong></p> <pre><code>public class Player { private String myPlayerName; private Weapon myWeapon; private int myCurrentHealth; private int myMaxHealth; private int myNumPotions; /** * Constructor initializing class Player * Parameters of the player should be: * player name, players initial health, the players weapon. */ public Player(String myPlayer, int initialHealth, Weapon currentWeapon) { myPlayerName = myPlayer; this.myWeapon = currentWeapon; myMaxHealth = 30; myCurrentHealth = initialHealth; myNumPotions = 0; } /** * Attack method which attacks opposing player * with current equipped weapon. */ public void attack(Player victim) { currentWeapon.hit(); if (boolean currentWeapon.hit() = true) { currentWeapon.damage(int dam); return dam; } Player victim.takeDamage(int damage); } } </code></pre> <p>and the weapon class:</p> <pre><code>import java.util.Random; public class Weapon { private int myHitProb; private int myMaxDamage; private Random myRNG; /** * Create a new weapon with a hit probability and damage */ public Weapon(int hitProb, int damage) { myHitProb = hitProb; myMaxDamage = damage; myRNG = new Random(); } public boolean hit() { int r = myRNG.nextInt(100); if (r &lt; myHitProb) { return true; } else { return false; } } public int damage() { int dam = myRNG.nextInt(myMaxDamage) + 1; return dam; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload