Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

is there anyone that can knows how to do this? thank you so much. Bigger problem

ID: 3625989 • Letter: I

Question

is there anyone that can knows how to do this? thank you so much.

Bigger problem – part 1
The following is some code for a video game. First, there is an Alien class that represents monsters:
public 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 = health;
this.name = name;
}
}

public class AlienPack {
private Alien aliens[];
public AlienPack ( int numAliens ) {
aliens = new Alien[ numAliens ];
}
public void addAlien ( Alien newAlien, in 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;
}
}



public class AlienPack {
private Alien aliens[];
public AlienPack ( int numAliens ) {
aliens = new Alien[ numAliens ];
}
public void addAlien ( Alien newAlien, in 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 code is not very object-oriented and does not support information hiding in the Alien class. Rewrite the code so that inheritance is used to represent the different types of aliens instead of the “type” parameter. This should result in deletion of the “type” parameter. Also rewrite the Alien class to hide the member variables and create a “getDamage” method for each derived class that returns the amount of damage the alien inflicts.
Rewrite the calculateDamage method to use getDamage and write a main method that tests the code.

Explanation / Answer