I am currently writing a small project for class that deals with inheritance and encapsulation. The parent class is as follows:
Code:
public class Alien
{
//public static enum AlienType { SNAKE_ALIEN, OGRE_ALIEN, MARSHMALLOW_MAN_ALIEN};
//public AlienType type;
private int health;
private String name;
public Alien()
{
this.name = "Daniel";
this.health = 50;
}
public Alien(String name)
{
this.name = name;
this.health = 50;
}
public Alien(int health)
{
this.name = "Daniel";
this.health = health;
}
public Alien(int health, String name)
{
this.health = health;
this.name = name;
}
public int getDamage()
{
return 15;
}
}
one of the derived classes is as follows:
Code:
public class Ogre extends Alien
{
private int damage;
public Ogre()
{
super();
damage = 6;
}
public Ogre(String name)
{
super(name);
damage = 6;
}
public Ogre(int health)
{
super(health);
damage = 6;
}
public Ogre(int health,String name)
{
super(health,name);
damage = 6;
}
public int getDamage()
{
return damage;
}
}
There are two other derived classes that only differ with name and damage value.
Then I have a class that will make a group of aliens and calculate the damage they do it is as follows:
Code:
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++)
{
damage = damage + aliens[i].getDamage();
/* if (aliens[i].type == Alien.AlienType.SNAKE_ALIEN)
{
damage += 10;
}
else if (aliens[i].type == Alien.AlienType.OGRE_ALIEN)
{
damage += 6;
}
else if (aliens[i].type == Alien.AlienType.MARSHMALLOW_MAN_ALIEN)
{
damage += 1;
}*/
}
return damage;
}
}
All of these class files compile fine. The one i am having problems with is when i try to add aliens to a list via the following code
Code:
public class AlienTest
{
Alien alien1 = new Ogre(100,"dan");
Alien alien2 = new Snake(100,"Bart");
Alien alien3 = new Marshmellow(10,"Jen");
AlienPack pack1 = new AlienPack(3);
pack1.addAlien(alien1,0);
/*pack1.addAlien(alien2,1);
pack1.addAlien(alien3,2);*/
}
I get an error of: AlienTest.java:9 <identifier> expected
pack1.addAlien(alien1,0);
I originally had Ogre alien1 = new Ogre(100,"Dan"); when declaring my variables and it also did not work. Any input into what is causing this would be greatly appreciated
Thanks
Andrew