-3

hello i'm super stuck and cannot work out why this is not working. i'm trying to use inheritance and override but i keep getting this error. Been trying to figure this out for the past hour but have had no clue. i'm probably missing something silly, any help would be hugely grateful.

public class FlyingDragon extends Entity {
private int Hp;
public FlyingDragon(int x, int y, int Hp){
    super (x, y);
    this.Hp = Hp;
}
public void setHp(int Hp){
    this.Hp = 100;
}

public int getHp(){
   return Hp;}

public void setType(String type) { ###error is here
 super.setType("Flying Dragon");}
}

    public abstract class Entity { 
private char symbol; // symbol that represents the entity
private String type; // every entity is of a type
private int x; // x coordinate in the room
private int y; // y coordinate in the room


 public Entity (int x, int y) {
 type = "entity";
 this.x=x;
 this.y =y;
}



 public char getSymbol() {
   return symbol;
  }

 public void setSymbol(char c) {
     symbol = c;
  }

 public int getX() {
   return x;
  }

  public int getY() {
   return y;
  }
  public void setX (int newx) {
     this.x=newx;

   }

   public void setY (int newy) {
     this.y=newy;

   }
    public String getType() {
      return type;
     }

  public void setType(String type) {
     this.type = type;
     }
  }

2 Answers2

0

Change method setType of FlyingDragon to :

public void setType(String type) {
        super.setType("Flying Dragon");
    }
Jay Smith
  • 2,107
  • 3
  • 11
  • 26
0

Try this:

Make separate file for Entity class put all Entity code into this class.

Entity Class:

public abstract class Entity { 
private char symbol; // symbol that represents the entity
private String type; // every entity is of a type
private int x; // x coordinate in the room
private int y; // y coordinate in the room


 public Entity (int x, int y) {
 type = "entity";
 this.x=x;
 this.y =y;
}



 public char getSymbol() {
   return symbol;
  }

 public void setSymbol(char c) {
     symbol = c;
  }

 public int getX() {
   return x;
  }

  public int getY() {
   return y;
  }
  public void setX (int newx) {
     this.x=newx;

   }

   public void setY (int newy) {
     this.y=newy;

   }
    public String getType() {
      return type;
     }

  public void setType(String type) {
     this.type = type;
     }
    }

Now create separate class for FlyingDragon.

FlyingDragon Class

public class FlyingDragon extends Entity {
private int Hp;
public FlyingDragon(int x, int y, int Hp){
    super(x, y);
    this.Hp = Hp;
}
public void setHp(int Hp){
    this.Hp = 100;
}

public int getHp(){
   return Hp;}

public void setType(String type) { //###error is here
 super.setType("Flying Dragon");}
}

Test Class:

  public class Test {

    public static void main(String[] args)  {

        FlyingDragon d = new FlyingDragon(1,2,3);

        //whatever you want to do here
    }
}
Omore
  • 604
  • 6
  • 18