java programming exercises with solutions
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:
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 the deletion of the “type” parameter.
![]() |
java programming exercises with solutions |
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 calculated damage method to use getDamage and write the main method that tests the code.
class Alien // create Alien class with required data members
{
protected int health;
protected String name;
public Alien(String name, int health) // constructor with name and health
{
this.name = name;
this.health = health;
}
// this method can be override in child classes
// here we create getDamage method in Alien class because we hold Alien class //
// reference in Alein pack so avoid complie time error .
public int getDamage() {
return 0;
}
}
class SNAKE_ALIEN extends Alien // child of alien
{
private final int damage = 10; // damage is 10
public SNAKE_ALIEN(String name, int hel) { // constructor with name and health {
super(name, hel); // calling super class constructor
}
@Override
public int getDamage() // override this method and return damage
{
return damage;
}
}
class OGRE_ALIEN extends Alien // same child class
{
private int damage = 6;
public OGRE_ALIEN(String name, int hel) // constructor with name and health
{
super(name, hel);// call super class constructor
}
@Override
public int getDamage()// return damage of this alien
{
return damage;
}
}
class MARSHMALLOW_MAN_ALIEN extends Alien // same as above
{
private final int damage = 1;
public MARSHMALLOW_MAN_ALIEN(String name, int hel) // constructor with name and health
{
super(name, hel);
}
@Override
public int getDamage() {
return damage;
}
}
class Alienpack {
private Alien[] aliens;
public Alienpack(int n) //constructor with one parameter
{
aliens=new Alien[n]; //create memory for n aliens }
public void addAlien(Alien newalien, int indx) // adding alien to alien pack
{
aliens[indx] = newalien;
}
public Alien[] getAliens()// get aliens
{
return aliens;
}
public int CalculateDamage() {
int totalDamage = 0; // this is our answer
for (int i = 0; i < aliens.length; i++) {
totalDamage += (aliens[i].getDamage());// add alien damage to
// total damage
}
return totalDamage; // return total damage
}
}
public class Main {public static void main(String[] args) {
Alienpack newpack = new Alienpack(4);// creating new alien pack with 4 aliens
// create 4 different aliens
SNAKE_ALIEN s1 = new SNAKE_ALIEN("cobra", 100);
OGRE_ALIEN o1 = new OGRE_ALIEN("OGRA443", 75);
MARSHMALLOW_MAN_ALIEN m1 = new MARSHMALLOW_MAN_ALIEN("Marsh77", 100);
SNAKE_ALIEN s2 = new SNAKE_ALIEN("Mamba", 100);
// add above aliens to pack newpack.addAlien(s1,0); newpack.addAlien(s2,1);
// newpack.addAlien(o1,2); newpack.addAlien(m1,3);
// print the total damage
System.out.println("Total Damage : " + newpack.CalculateDamage());
}
}