Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Does anyone know how to solve this? The following is some code designed by J. Hacker for a video game. There is an Alien class

Does anyone know how to solve this?

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 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 = 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 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 instance variables and create a getDamage method for each derived class that returns the amount of damage the alien inflicts. 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 inflicts. (3) Derived class: OgreAlien.java Constructor without any parameters. Constructor with two parameters: health and name. getDamage returns the amount of damage this alien inflicts. (4) Derived class: MarshmallowAlien.java Constructor without any parameters. Constructor with two parameters: health and name. getDamage returns the amount of damage this alien inflicts

(5) pack class: AlienPack.java add calculateDamage() method to the existing AlienPack class. To calculate the damage inflicted 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());

Run your program make sure it is working correctly

(1) Alien.java,

(2) SnakeAlien.java,

(3) OgreAlien.java,

(4) MarshmallowAlien.java,

(5) AlienPack.java,

(6) AlienDemo.java.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions