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
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
