-1

I get a lot of errors over what should be a simple constructor for a fighter class. Furthermore, they seem to be illegal start of expression errors or ; expected errors, for the most part.

This is my code:

class Fighter(String name, int health, int attack, int defense){
   //default constructor
   //input constructor

      private String name;
      private int health;
      private int attack;
      private int defense;

      private String setName(){
         this.name = name;
      }

      public String getName(){
         return name;
      }

      private int setHealth(){
         this.health = health;
      }

      public int getHealth(){
         return health;
      }

      private int setAttack(){
         this.attack = attack;
      }

      public int getAttack(){
         return attack;
      }

      private int setDefense(){
         this.defense = defense;
      }

      public int getDefense(){
         return defense;
      }
    }

What am I doing to generate all of these errors? Is it related to private or public methods?

Sweeper
  • 213,210
  • 22
  • 193
  • 313
Gary Theurpe
  • 63
  • 1
  • 5

2 Answers2

0

Methods go in the class, not the constructor. I think you wanted something like,

class Fighter {
    public Fighter(String name, int health, int attack, int defense) {
        setName(name);
        setHealth(health);
        setAttack(attack);
        setDefense(defense);
    }

    private String name;
    private int health;
    private int attack;
    private int defense;

    private void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public int getHealth() {
        return health;
    }

    private void setAttack(int attack) {
        this.attack = attack;
    }

    public int getAttack() {
        return attack;
    }

    private void setDefense(int defense) {
        this.defense = defense;
    }

    public int getDefense() {
        return defense;
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

That's not how you define a constructor. A constructor is called when the object is being created (new Fighter()). It is a special kind of method so you can only have statements in it. This:

private String setName(){
     this.name = name;
}

is not a statement, so that's why there are errors. The above is another seperate method all together. You should define this kind of stuff in a class, not in the constructor. Pull them out like this:

public class Fighter {
    public Fighter(String name, int health, int attack, int defense) {
        setName(name);
        setHealth(health);
        setAttack(attack);
        setDefense(defense);
    }

    private String name;
    private int health;
    private int attack;
    private int defense;

    private void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    public int getHealth() {
        return health;
    }

    private void setAttack(int attack) {
        this.attack = attack;
    }

    public int getAttack() {
        return attack;
    }

    private void setDefense(int defense) {
        this.defense = defense;
    }

    public int getDefense() {
        return defense;
    }
}

Now all the methods are outside the constructor. The fields, methods and the constructor(s) are components of a class, not that of a constructor.

Sweeper
  • 213,210
  • 22
  • 193
  • 313