-2

I have a class called Hero Where I create DrowRanger.

I am using setters and getters and I want to return some of the stats for DrowRanger.

Since I implement an interface containing these methods, then define each method in this class, is there not a way to just return the method getHealth() but just DrowRanger's Health?

like DrowRanger.getHealth(); or somthing like that?

This is the Hero class:

public class Hero implements NPC {

        private int level;
        private String primaryAttribute;
        private String attackType;
        private String ability1;
        private String ability2;
        private String ability3;
        private String ability4;
        private double strength;
        private double strengthMultiplier;
        private double agility;
        private double agilityMultiplier;
        private double intelligence;
        private double intelligenceMultiplier;
        private int health;
        private int mana;
        private int damageMin;
        private int damageMax;
        private int range;
        private double armor;
        private int movement;

        //default constructor
        public Hero(
                int level,
                String primaryAttribute,
                String attackType,
                String ability1,
                String ability2,
                String ability3,
                String ability4,
                double strength,
                double strengthMultiplier,
                double agility,
                double agilityMultiplier,
                double intelligence,
                double intelligenceMultiplier,
                int health,
                int mana,
                int damageMin,
                int damageMax,
                int range,
                double armor,
                int Movement
                     ){

        } // End Constructor

        public static void main (String[] args) {
            getHealth(DrowRanger);

            } // End Main Method

        private static Object DrowRanger() {
        Hero DrowRanger = new Hero(
              0,
              "Agility",
              "Ranged",
              "Frost Arrows",
              "Gust",
              "Precision Aura",
              "Marksmanship",
              17,
              1.9,
              26,
              1.9,
              15,
              1.4,
              473,
              195,
              44,
              55,
              625,
              0.64,
              300);

        return DrowRanger;
} // End DrowRanger Method


        // getters and setters - required to implement ALL from interface
        public int getLevel() {
            return this.level;
        }

        public String getPrimaryAttribute() {
            return this.primaryAttribute;
        }

        public String getAttackType() {
            return this.attackType;
        }

        public String getAbility1() {
            return this.ability1;
        }

        public String getAbility2() {
            return this.ability2;
        }

        public String getAbility3() {
            return this.ability3;
        }

        public String getAbility4() {
            return this.ability4;
        }

        public double getStrength() {
            return this.strength;
        }

        public double getStrengthMultiplier() {
            return this.strengthMultiplier;
        }

        public double getAgility() {
            return this.agility;
        }

        public double getAgilityMultiplier() {
            return this.agilityMultiplier;
        }

        public double getIntelligence() {
            return this.intelligence;
        }

        public double getIntelligenceMultiplier() {
            return this.intelligenceMultiplier;
        }

        public int getHealth() {
            return this.health;
        }

        public int getMana() {
            return this.mana;
        }

        public int getDamageMin() {
            return this.damageMin;
        }

        public int getDamageMax() {
            return this.damageMax;
        }

        public int getRange() {
            return this.range;
        }

        public double getArmor() {
            return this.armor;
        }

        public int getMovement() {
            return this.movement;
        }

    // This is where the setters are.

        public void setLevel(int level) {
            this.level = level;
        }

        public void setPrimaryAttribute(String primaryAttribute) {
            this.primaryAttribute = primaryAttribute;
        }

        public void setAttackType(String attackType) {
            this.attackType = attackType;
        }

        public void setAbility1(String ability1) {
            this.ability1 = ability1;
        }

        public void setAbility2(String ability2) {
            this.ability2 = ability2;
        }

        public void setAbility3String(String ability3) {
            this.ability3 = ability3;
        }

        public void setAbility4(String ability4) {
            this.ability4 = ability4;
        }

        public void setStrength(double strength) {
            this.strength = strength;
        }

        public void setStrengthMultiplier(double strengthMultiplier) {
            this.strengthMultiplier = strengthMultiplier;
        }

        public void setAgility(double agility) {
            this.agility = agility;
        }

        public void setAgilityMultiplier(double agilityMultiplier) {
            this.agilityMultiplier = agilityMultiplier;
        }

        public void setIntelligence(double intelligence) {
            this.intelligence = intelligence;
        }

        public void setIntelligenceMultiplier(double intelligenceMultiplier) {
            this.intelligenceMultiplier = intelligenceMultiplier;
        }

        public void setHealth(int health) {
            this.health = health;
        }

        public void setMana(int mana) {
            this.mana = mana;
        }

        public void setDamageMin(int damageMin) {
            this.damageMin = damageMin;
        }

        public void setDamageMax(int damageMax) {
            this.damageMax = damageMax;
        }

        public void setRange(int range) {
            this.range = range;
        }

        public void setArmor(double armor) {
            this.armor = armor;
        }

        public void setMovement(int movement) {
            this.movement = movement;
        }

    } // End Character Class
Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
wuno
  • 9,547
  • 19
  • 96
  • 180
  • 2
    The [last time you posted this code](http://stackoverflow.com/q/24985990/2071828) you were asked for format it and comply with Java naming conventions. Not correcting this and posting again is not ideal. – Boris the Spider Jul 27 '14 at 22:49
  • I did comply, this is a complete different question. Feel free to read it without blindly disapproving it. Thanks! – wuno Jul 27 '14 at 22:50
  • 1
    I did read it. Carefully. You still have an instance variable in `PascalCase` - `DrowRanger`. You still have wrong indentation. Please use an IDE and format your code. – Boris the Spider Jul 27 '14 at 22:51
  • I'm guessing you'll want to change the signature of DrowRanger() to: `public static Hero drowRanger()`... – Hans Brende Jul 27 '14 at 22:55
  • Also your Hero constructor does not actually initialize any variables... – Hans Brende Jul 27 '14 at 22:56
  • What do you mean about initialize my variables in the constructor? – wuno Jul 27 '14 at 23:00
  • You constructor has no body. You take variables in the signature but don't use them in the body at all - those variables are just discarded. You need to add, for example, `this.level = level` for each variable. – Boris the Spider Jul 27 '14 at 23:07
  • So even though I have done that in all of my Getters below I need to do it again in the constructor? – wuno Jul 27 '14 at 23:10

1 Answers1

1

Does this work?

public static void main(String[] args) {
    ((Hero) DrowRanger()).getHealth(); // This gets a drowRanger with the "DrowRanger()" method and calls "getHealth()" on it
}

In addition you should try to follow Java naming conventions which are: variables and methods start with lower case letter and objects and class names start with an upper case letter.

You could also make DrowRanger() return a Hero instead of an Object if you don't want to cast it

Zach
  • 4,652
  • 18
  • 22
  • In eclipse it gives me and error on getHealth. It offers to add cast to DrowRanger... – wuno Jul 27 '14 at 22:53
  • @BoristheSpider would you mind helping instead of down voting the question and the only answer then criticizing everyone. – wuno Jul 27 '14 at 22:55
  • How doesn't it compile? – Zach Jul 27 '14 at 22:56
  • I'm sorry, I must be missing something, I see a closing parenthesis for each opening one, where is the missing parenthesis? – Zach Jul 27 '14 at 23:00
  • Basically this should be `((Hero) DrowRanger()).getHealth()`. – Boris the Spider Jul 27 '14 at 23:04
  • The error is on (Hero), I think cause I am need to initialize Hero in the constructor? – wuno Jul 27 '14 at 23:05
  • Oh thank you Boris, I was not aware that my method of casting was not valid, sorry I'll fix it now – Zach Jul 27 '14 at 23:06
  • In the future, please verify your code before posting it as an answer. – Boris the Spider Jul 27 '14 at 23:08
  • Ok thank you that compiles without errors but it does not print the health to the console. – wuno Jul 27 '14 at 23:09
  • @Boris the Spider According to Eclipse this compiles just fine `int x = (int) ('c');` so I thought my method would work. @NichoDiaz, if you want it to print you have to put it inside a `println` method: `System.out.println(((Hero) DrowRanger()).getHealth());` – Zach Jul 27 '14 at 23:10