Asked by Java Programming
I am trying to create a Monster class for a project. My attributes are healthPoints, damage, description, and type (where type is an enum). Currently, it has a default constructor, a full constructor, and a constructor which accepts only the enum monsterType.
My issue is here: In the driver, using the random class, a random monsterType is chosen, and a new Monster is created. However, healthPoints, damage, and description all print to the screen as 0 or null.
I believe the issue is in my setters for each of those attributes. I have a switch statement in each setter, to set a certain number of healthPoints, etc, based on what type of monster is randomly generated. Here is my healthPoints setter as an example:
public void setHealthPoints(int healthPoints)
{
switch(type)
{
case DROW:
this.healthPoints = 30;
break;
case LICH:
this.healthPoints = 40;
break;
case ORC:
this.healthPoints = 20;
break;
case OWLBEAR:
this.healthPoints = 20;
break;
case RUST_MONSTER:
this.healthPoints = 10;
break;
}
this.healthPoints = healthPoints;
}
I'm not sure how much help I can get without showing the full code, but any suggestions as to what I could do to get this to work would be much appreciated! Thank in advance!
My issue is here: In the driver, using the random class, a random monsterType is chosen, and a new Monster is created. However, healthPoints, damage, and description all print to the screen as 0 or null.
I believe the issue is in my setters for each of those attributes. I have a switch statement in each setter, to set a certain number of healthPoints, etc, based on what type of monster is randomly generated. Here is my healthPoints setter as an example:
public void setHealthPoints(int healthPoints)
{
switch(type)
{
case DROW:
this.healthPoints = 30;
break;
case LICH:
this.healthPoints = 40;
break;
case ORC:
this.healthPoints = 20;
break;
case OWLBEAR:
this.healthPoints = 20;
break;
case RUST_MONSTER:
this.healthPoints = 10;
break;
}
this.healthPoints = healthPoints;
}
I'm not sure how much help I can get without showing the full code, but any suggestions as to what I could do to get this to work would be much appreciated! Thank in advance!
Answers
Answered by
Linds
I just realized I got my name and the subject mixed up...I also realized I don't have a default case in my switch statement. Not sure if that will change anything, but I'll try it.
There are no AI answers yet. The ability to request AI answers is coming soon!
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.