The following is some code designed by J. Hacker for a video game. There is an c
ID: 3837341 • Letter: T
Question
The following is some code designed by J. Hacker for a video game. There is an class to a monster and an Alienpack class that represents a band of aliens and how much damage they can inflict: 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;//Stores one of the three above types public int health 0-dead, 100-full strength public String name public Alien (int type, int health String name this type type this health this name name public cla Alien Pack private Alien aliens public Alien Pack (int numAliens) aliens new Alien (numaliens) public void addAlien lien newAlien, int index aliens Tindex new Alien public Alien. getAliens return aliens public int calculateDamage int damage for (int i = 0; i iExplanation / Answer
abstract class Alien
{
int health;
String name;
Alien(int h, String n)
{
health=h;
name=n;
}
public String getName()
{
return name;
}
public abstract int getDemage();
}
class SnakeAlien extends Alien
{
SnakeAlien(int h, String n)
{
super(h,n);
}
public int getDemage()
{
return 10;
}
}
class OgreAlien extends Alien
{
OgreAlien(int h, String n)
{
super(h,n);
}
public int getDemage()
{
return 6;
}
}
class MarshmallowManAlien extends Alien
{
MarshmallowManAlien(int h, String n)
{
super(h,n);
}
public int getDemage()
{
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 getDemage()
{
int demage=0;
for(int i=0; i<aliens.length;i++)
{
demage+=aliens[i].getDemage();
}
return demage;
}
public static void main(String s[])
{
AlienPack p= new AlienPack(3);
p.addAlien(new SnakeAlien(100,"aSnake"),0);
p.addAlien(new OgreAlien(100,"True Ogre"),1);
p.addAlien(new MarshmallowManAlien(100,"MM"),2);
Alien aliens[];
aliens= p.getAliens();
System.out.println("Aliens attacked ");
for(int i=0; i<aliens.length;i++)
{
System.out.println(aliens[i].getName());
}
System.out.println("Total demage caused: "+p.getDemage());
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.