Alien class below needs to be rewritten from an inheritance class to an abstract
ID: 3641305 • Letter: A
Question
Alien class below needs to be rewritten from an inheritance class to an abstract class.class alien
{
public static final int SNAKE_ALIEN = 0;
public static final int OGRE_ALIEN = 1;
public static final int MARSHMALLOW_MAN_ALIEN = 2;
public int type;
public int health; // 0=dead, 100=full strength
public String name;
public Alien(int type, int health, String name)
{
this.type = type;
this.health = health;
this.name = name;
}
}
public class AlienPack
{
private Alien[] aliens;
public AlienPack(int numAliens)
{
aliens = new Aliens[numAliens];
}
public void addAlien(Alien newAlien, int index)
{
aliens[index] = newAlien;
}
public Alien[] getAliens()
{
return aliens;
}
public int calculateDamage()
{
int damage = 0;
for( int i=0; i<aliens.length; i++ )
{
if (aliens[i].type==Alien.SNAKE_ALIEN)
{
damage +=10;
}
else if( aliens[i].type==Alien.OGRE_ALIEN)
{
damage +=6;
}
else if( aliens[i].type==Alien.MARSHMALLOW_MAN_ALIEN)
{
damage +=1;
}
}
return damage;
}
}
The rewritten Alien class should be made abstract since there will never be a need to create an instance of it, only its derived classes. Change this to an abstract class and also make the getDamage method an abstract method. So basically the whole Alien class and getDamage need to be made abstract.
Explanation / Answer
I have rewritten the alien class to be abstract and the getDamage method as abstract. There are 3 child classes inherits the abstract class and return the damage number in the getDamage method. Following is the rewritten Alien and AlienPack class abstract class Alien { public int type; public int health; // 0=dead, 100=full strength public String name; public abstract int getDamage(); } class SNAKE_ALIEN extends Alien { public int getDamage() { return 10; } } class OGRE_ALIEN extends Alien { public int getDamage() { return 6; } } class MARSHMALLOW_MAN_ALIEN extends Alien { public int getDamage() { return 1; } } class AlienPack { private Alien[] aliens; public AlienPack(int numAliens) { aliens = new Alien[numAliens]; } public void addAlien(Alien newAlien, int index) { aliens[index] = newAlien; } public Alien[] getAliens() { return aliens; } public int calculateDamage() { int damage = 0; for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.