Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 20th, 2005, 12:38 AM
Moleme Moleme is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 2 Moleme User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 6 sec
Reputation Power: 0
Java compile error Identifer expected

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

Reply With Quote
  #2  
Old October 20th, 2005, 03:54 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 748 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 13 h 56 m 25 sec
Reputation Power: 4
You are executing code in the AlienTest class file itself. You should only put declarations there. If you add a main method to AlienTest and put the code in that method then it will compile. Need an example?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Java compile error Identifer expected


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT