Ant farm Program

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

72 Replies - 4549 Views - Last Post: 13 June 2012 - 10:52 AM Rate Topic: -----

#1 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Ant farm Program

Posted 06 June 2012 - 01:15 PM

Hey i'm working on a program where I need to create to ant colonies (red and black) that battle each other. I'm having some trouble with creating the ants.

These are my instructions for creating the ants:

Constructor – The constructor should create an ant and give it strength. You can decide how to assign strength. However, one easy way would be to use the built in Random class in order to make the fights fair. Remember, if you aren’t sure how to use a class, read the documentation, post questions to the discussion thread.
int getStrength() - This method should return the ant's strength which should be represented using an integer data member. The ant's strength will be used to determine how an ant performs during a battle. Ants with a high strength value should kill ants with a lower strength value.
IAntBase breed(IAntBase mate) - The breed function should create a new ant whose strength value is calculated based on the strength of its parents. For example all baby ants could be created with the strength of the strongest parent or they could be created with the average strength of the two parents. This way, offspring of strong parents will also be strong. You can be creative if you like and come up with your own way of implementing the breed method.
boolean fight(IAntBase competitor) - This function should compare the competitor's strength to the strength of the current ant ('this'). The strongest ant should win the fight. If the competitor wins then this function would return false otherwise it should return true.


I'm not really sure how to start this, it seems that all of the methods do not have bodies which I have not used yet. Does the code in the IAntBase class need modification or do I create the class that implements it and add code to that? I tried starting with creating an AntBase class that implements IAntBase but it keeps giving me an error.

Here is the template code given to me that is to create the ants:

public interface IAntBase {
	public void ant();
	
    /**
     * The breed method produces a new ant based on this ant
     * and the ant passed as a parameter.
     * 
     * @param mate the ant to use as the other parent for the new child ant.
     * @return returns a new child ant based on this ant and mate.
     */
    public IAntBase breed(IAntBase mate);
   
    /**
     * The fight method makes this ant fight with another ant.
     * 
     * @param enemy the ant that this ant should fight.
     * @return true if this ants wins otherwise false.
     */
    public boolean fight(IAntBase enemy);

    /**
     * Returns the strength of this ant.
     * 
     * @return an integer representing this ant's strength.
     */
    public int getStrength();
}

class AntBase implements IAntBase{
	
}


Is This A Good Question/Topic? 0
  • +

Replies To: Ant farm Program

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Ant farm Program

Posted 06 June 2012 - 01:28 PM

You are getting the error because you have no implemented the methods defined in the IAntBase interface in the implementing class AntBase.

Think of an interface as a kind of contract. You define methods in the interface (just the headers, no body) that all classes that implement the interface must implement ( (the bodies) if that makes sense).

So in this case IAntBase says that all implementing classes (AntBase) must define methods called ant(), breed(), fight() and getStrength().

Hopefully that all makes sense.
Was This Post Helpful? 0
  • +
  • -

#3 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Ant farm Program

Posted 06 June 2012 - 01:40 PM

Yea we're just learning about it so my understanding is not that great yet. I am getting the error where it says it must define the methods I'm just not sure how to fix this. How would I set up the AntBase class?
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Ant farm Program

Posted 06 June 2012 - 01:40 PM

You implement the methods.

class AntBase implements IAntBase{
    @Override
    public IAntBase breed(IAntBase mate)
    {
          // give the method a body and so whatever
    }

   // do the same for all methods in the interface

}


Interfaces

This post has been edited by Ryano121: 06 June 2012 - 01:54 PM

Was This Post Helpful? 2
  • +
  • -

#5 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Ant farm Program

Posted 06 June 2012 - 01:58 PM

oh ok I see what you mean. Thanks, I'll start doing that and see how far I get :wink:
Was This Post Helpful? 0
  • +
  • -

#6 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Ant farm Program

Posted 06 June 2012 - 02:13 PM

ok so I did this:

class AntBase implements IAntBase
{
	public IAntBase breed(IAntBase mate)
	{
		
	}
	public boolean fight(IAntBase enemy)
	{
		
	}
	public int getStrength()
	{
		int strength = 0;
		return strength;
	}
}
class RedAnt extends AntBase
{
	
}
class BlackAnt extends AntBase
{
	
}


The first part of the instructions is saying the constructor needs to create the ant and give in strength. Do I create this in the AntBase class? Not sure how to create the ant or give it a randome strength level. Would creating it be like AntBase ant = new AntBase();?
Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Ant farm Program

Posted 06 June 2012 - 02:16 PM

Aren't you supposed to also have an 'Ant' class?
Was This Post Helpful? 0
  • +
  • -

#8 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Ant farm Program

Posted 06 June 2012 - 02:20 PM

No sorry I put that in myself, it wasn't part of the template and I didn't want to confuse myself even more so I took it out.
Was This Post Helpful? 0
  • +
  • -

#9 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Ant farm Program

Posted 06 June 2012 - 04:05 PM

how do I give the created ants a random strength number in the constructor?

class AntBase implements IAntBase
{
	public AntBase()
	{
		AntBase ant = new AntBase();
		
	}
	public IAntBase breed(IAntBase mate)
	{
		
	}
	public boolean fight(IAntBase enemy)
	{
		
	}
	public int getStrength()
	{
		int strength = 0;
		return strength;
	}
}

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,806
  • Joined: 27-December 08

Re: Ant farm Program

Posted 06 June 2012 - 04:07 PM

Do not include this line in your constructor: AntBase ant = new AntBase();. It will create a StackOverflowError. Also, strength should be an instance variable, not a local variable in a method. And did you look at the Random class like your instructor suggested?
Was This Post Helpful? 1
  • +
  • -

#11 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Ant farm Program

Posted 06 June 2012 - 04:16 PM

Ok but then how do I create the ant in the constructor if i take that out? Also do I need to set the numbers so there is a min and max (such as 1 to 10)? And after creating the ant would this give it a random strength: ant.getStrength()= math.random();
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,806
  • Joined: 27-December 08

Re: Ant farm Program

Posted 06 June 2012 - 04:24 PM

I think the AntBase class is supposed to model your Ant.

Quote

ant.getStrength()= math.random();

This syntax is illegal. Just assign the Random value to the strength instance variable. And use the Random class, as it is easier for generating Random ints. The Math class has a capital 'M' as well, not a lowercase 'm.'

The major point of the constructor is to initialize the instance variables of your Object.
Was This Post Helpful? 0
  • +
  • -

#13 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Ant farm Program

Posted 06 June 2012 - 04:38 PM

So what do I put in the constructor to "model the ant"?
I tried doing this for strength but it says it can't convert from double to int and my instructor set it up as int. Also it says to give it strength in the constructor:

class AntBase implements IAntBase
{
private static int strength;
public int getStrength()
	{
		strength = Math.random();
		return strength;
	}
}


Can you show me what to do with the strength for the constructor, not really sure what you mean?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10469
  • View blog
  • Posts: 38,806
  • Joined: 27-December 08

Re: Ant farm Program

Posted 06 June 2012 - 04:44 PM

A few points:
  • Use of the static keyword makes strength a global, not an instance variable. Globals are associated with the class, not the object, and hold the same value regardless of the instance of the class. You do not need it here, as strength should be unique to the individual instances of the AntBase class.

  • The Random class is easier to use for generating Random ints. I've said this twice already. Math.random() returns a double. You would have to deal with ranges. The Random nextInt() method makes generating strengths easy. Look into it and I strongly encourage using it, as does your professor.

  • Quote

    Can you show me what to do with the strength for the constructor, not really sure what you mean?

    Do you know how to assign a variable a value? That is literally what you do in the constructor. The getter method should just return the (value in the) variable, not assign the variable a new value.

Was This Post Helpful? 0
  • +
  • -

#15 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Ant farm Program

Posted 06 June 2012 - 05:11 PM

so do I need to create an array from 1 to 10 in the constructor and assign strength in the constructor to random nextint() or something? I don't know how to do this.
Was This Post Helpful? 0
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »