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

1 Project Description The following is some code designed by J. Hacker for a vid

ID: 3587108 • Letter: 1

Question

1 Project Description
The following is some code designed by J. Hacker for a video game. There is an Alien class to represent a monster and an AlienPack class that represents a band of aliens and how much damage they can inict:


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]; }
1
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;// Snake does 10 damage } else if (aliens[i].type==Alien.OGRE_ALIEN) { damage +=6;// Ogre does 6 damage } else if (aliens[i].type== Alien.MARSHMALLOW_MAN_ALIEN) { damage +=1; // Marshmallow Man does 1 damage} } 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 dierent 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 instance variables and create a getDamage method for each derived class that returns the amount of damage the alien inicts. Finally, rewrite the calculateDamage method to use getDamage and write a main method that tests the code.

2 Develop Classes

(1) Base class: Alien.java – Constructor without any parameters. – Constructor with two parameters: health and name. – Accessors and mutators for name and health.

• (2) Derived class: SnakeAlien.java – Constructor without any parameters. – Constructor with two parameters: health and name. – getDamage returns the amount of damage this alien inicts. •

(3) Derived class: OgreAlien.java – Constructor without any parameters. – Constructor with two parameters: health and name. – getDamage returns the amount of damage this alien inicts. •

(4) Derived class: MarshmallowAlien.java – Constructor without any parameters. – Constructor with two parameters: health and name. – getDamage returns the amount of damage this alien inicts. •

(5) pack class: AlienPack.java – add calculateDamage() method to the existing AlienPack class. – To calculate the damage inicted by all aliens in the pack we can now simply iterate through each alien and call its getDamage() method •

(6) Driver class: AlienDemo.java – Create an object of OgreAlien class type. – Create an object of SnakeAlien class type. – Create an object of MarshmallowAlien class type. – Create a pack AlienPack to save the above three objects. – Print out the total damage.
Sample code inside main method:

OgreAlien brutus = ; // please finish this SnakeAlien slimy = ; // please finish this MarshmallowAlien puffy = ; // please finish this
AlienPack pack = ; // 3 aliens in the pack pack.addAlien(brutus, 0); pack.addAlien(slimy, 1); pack.addAlien(puffy, 2);
System.out.println("Total pack damage = " + pack.calculateDamage());


3 Submission
Run your program make sure it is working correctly. Please submit all your source les to blackboard:
(1) Alien.java, (2) SnakeAlien.java, (3) OgreAlien.java, (4) MarshmallowAlien.java, (5) AlienPack.java, (6) AlienDemo.java

COULD YOU PLEASE COMMENT ON EACH SECTION FOR CLEARLY KNOWING WHAT CODE FUNCTION IS AND ALSO COULD YOU PLEASE ADD LIKE WHEN I RUN CODE I SHOULD GET MY NAME FIRST AND STUDENT ID AND THEN IT SHOULD ASK FOR RUN CODE

Explanation / Answer

public abstract class Alien {

public int health;

public String name;

public Alien() {

// TODO Auto-generated constructor stub

}

/**

* @param health

* @param name

*/

public Alien(int health, String name) {

this.health = health;

this.name = name;

}

/**

* @return the health

*/

public int getHealth() {

return health;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param health

* the health to set

*/

public void setHealth(int health) {

this.health = health;

}

/**

* @param name

* the name to set

*/

public void setName(String name) {

this.name = name;

}

// ABSTRACT METHOD

public abstract int getDamage();

}

public class SnakeAlien extends Alien {

public SnakeAlien() {

// TODO Auto-generated constructor stub

}

/**

* @param health

* @param name

*/

public SnakeAlien(int health, String name) {

super(health, name);

// TODO Auto-generated constructor stub

}

/*

* (non-Javadoc)

*

* @see Alien#getDamage()

*/

public int getDamage() {

return 10;

}

}

public class OgreAlien extends Alien {

public OgreAlien() {

// TODO Auto-generated constructor stub

}

/**

* @param health

* @param name

*/

public OgreAlien(int health, String name) {

super(health, name);

// TODO Auto-generated constructor stub

}

/*

* (non-Javadoc)

*

* @see Alien#getDamage()

*/

public int getDamage() {

return 6;

}

}

public class MarshmallowAlien extends Alien {

public MarshmallowAlien() {

// TODO Auto-generated constructor stub

}

/**

* @param health

* @param name

*/

public MarshmallowAlien(int health, String name) {

super(health, name);

// TODO Auto-generated constructor stub

}

/*

* (non-Javadoc)

*

* @see Alien#getDamage()

*/

public int getDamage() {

return 1;

}

}

public class AlienPack {

private Alien[] aliens;

/**

* CREATE NUM OF ALIENS ARRAY WITH NUMALIENS

*

* @param numAliens

*/

public AlienPack(int numAliens) {

aliens = new Alien[numAliens];

}

/**

* METHOD TO ADD ALIEN TO THE INDEX

*

* @param newAlien

* @param index

*/

public void addAlien(Alien newAlien, int index) {

aliens[index] = newAlien;

}

/**

* RETURN THE ALIENS

*

* @return

*/

public Alien[] getAliens() {

return aliens;

}

/**

* METHOD TO CALCULATE DAMAGE

*

* @return

*/

public int calculateDamage() {

int damage = 0;

for (Alien alien : aliens) {

damage += alien.getDamage();

}

return damage;

}

}

public class AlienDemo {

public static void main(String[] args) {

String name = "Raj Kumar";

String Id = "34222";

System.out.println("Student Name:" + name);

System.out.println("Student ID:" + Id);

// CREATE OBJECTS OF ALL CLASSES

OgreAlien brutus = new OgreAlien(10, "brutus");

SnakeAlien slimy = new SnakeAlien(20, "slimy");

MarshmallowAlien puffy = new MarshmallowAlien();

AlienPack pack = new AlienPack(3); // 3 aliens in the pack

// ADD THEM TO PACK

pack.addAlien(brutus, 0);

pack.addAlien(slimy, 1);

pack.addAlien(puffy, 2);

// PRINT THE DAMAGE

System.out.println("Total pack damage = " + pack.calculateDamage());

}

}

OUTPUT:

Student Name:Raj Kumar

Student ID:34222

Total pack damage = 17