Search code examples
javabluej

How to call upon a variable from another object of the same class


import java.util.Random;
public class Character
{
    // instance variables - vervang deze door jouw variabelen
    private String _name;
    private double _level;
    private double _strenght;
    private double _speed;
    private double _defense;
    private double _currenthp;
    private double _maxhp;

    /**
     * Voorbeeld van een method - schrijf hier jouw comment
     *
     * @param  y    deze method krijgt deze parameter mee in de aanroep
     * @return    deze method geeft de som van x en y terug
     */
    public Character()
    {
    }

    public Character(String name,double level)
    {
        // schrijf hier jouw code
        _name = name;
        _level = level;
        Random rand = new Random();
        //level voor het berekenen van skills
        int lvl = (int)level;
        //stenght en speed parameter voor random
        int stsplvl = 12*lvl - 8*lvl;
        //defense parameter voor random
        int deflvl = 6*lvl - 4*lvl;
        //hp parameter voor random
        int hplvl = 30*lvl - 20*lvl;
        //Strenght berekenen
        double st = (double)rand.nextInt(stsplvl);
        st = st + 8*lvl;
        _strenght = st;
        //Speed berekenen
        double sp = (double)rand.nextInt(stsplvl);
        sp = sp + 8*lvl;
        _speed = sp;
        //Defense berekenen
        double def = (double)rand.nextInt(deflvl);
        def = def + 4*lvl;
        _defense = def;
        //Hp berekenen
        double hp = (double)rand.nextInt(hplvl);
        hp = hp + 20*lvl;
        _currenthp = hp;
        _maxhp = hp;
    }
    public void Character(String name,double level)
    {
        _name = name;
        _level = level;
    }
    public void ploth()
    {
        System.out.println(_name + ": " + _currenthp + " / " + _maxhp + " HP");
    }
    public int attack(Character _other)
    {
        int defe = super (double._other _defense);
        Random rando = new Random();
        //_strenght is een double
        int damcalc = (int)(1.2 * _strenght - 0.8 * _strenght);
        int netdam = rando.nextInt(damcalc);
        int totdam = (int)(netdam + 0.8*_strenght);
        int enddam = totdam - defe;
    }
}

So in the last return statement I am trying to get the defense variable of the other object (character that is being attacked). I am trying to take a random number based on the strength stat of this character and then to create a damage stat. After that I'd do damage - defense (from the other character). Does anyone know what code I use for this? PS:Please don't cringe this is the stuff we learn at school these days.


Solution

  • From your description you want your method attack return enddam like below:

    public int attack(Character _other)
    {
        int defe = (int)_other._defense;
        Random rando = new Random();
        //_strenght is een double
        int damcalc = (int)(1.2 * _strenght - 0.8 * _strenght);
        int netdam = rando.nextInt(damcalc);
        int totdam = (int)(netdam + 0.8*_strenght);
        int enddam = totdam - defe;
        return enddam;
    }
    

    Delete the void method Character, if it is a constructor you already have one with the same signature.