SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Inheritance
Exercise:
Programming Projects
Question:3 | ISBN:9780132830317 | Edition: 5

Question

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];

}


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.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

public class Alien {

   protected int health;
   protected String name;
   
   Alien(){
       
   }
   
   Alien(int health,String name)
   {
     
       this.health=health;
       this.name=name;
   }
           
   
   
   
}
public class MarshmallowAlien extends Alien {
    private final int damage=1;
    private int number;
    MarshmallowAlien(int number,int health, String name)
    {
        super(health,name);
        this.number=number;
    }
    
    
    int getDamage(){
        return damage;
    }
    
    int calculateDamage(){
        return getDamage()*number;
        
        
    }
    
    public String toString(){
        return ("The Alien "+name+" with Health "+health+" caused Damage "+calculateDamage());
    }
}
public class OgreAlien extends Alien{
    private final int damage=6;
    private int number;
    OgreAlien(int number,int health, String name)
    {
        super(health,name);
        this.number=number;
    }
    
    
    int getDamage(){
        return damage;
    }
    
    int calculateDamage(){
        return getDamage()*number;
        
        
    }
    
    public String toString(){
        return ("The Alien "+name+" with Health "+health+" caused Damage "+calculateDamage());
    }
}
public class SnakeAlien extends Alien {
    private final int damage=10;
    private int number;
    SnakeAlien(int number,int health, String name)
    {
        super(health,name);
        this.number=number;
    }
    
    
    int getDamage(){
        return damage;
    }
    
    int calculateDamage(){
        return getDamage()*number;
        
        
    }
    
    public String toString(){
        return ("The Alien "+name+" with Health "+health+" caused Damage "+calculateDamage());
    }
    
    
}

public class TestMethod {

  
    public static void main(String[] args) {
        SnakeAlien alien1= new SnakeAlien(50,78,"Snake");
        OgreAlien alien2= new OgreAlien(22,99,"Ogre");
        MarshmallowAlien alien3=new MarshmallowAlien(44,100,"Marshmallow Alien");
        System.out.println(alien1.toString());
        System.out.println(alien2.toString());
        System.out.println(alien3.toString());
    }
    
}

Output

The Alien Snake with Health 78 caused Damage 500
The Alien Ogre with Health 99 caused Damage 132
The Alien Marshmallow Alien with Health 100 caused Damage 44

1 0

Discussions

Post the discussion to improve the above solution.