Ant farm Program

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5

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

#31 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 07 June 2012 - 01:28 PM

Why do you want an instance of AntBase in AntBase?
Was This Post Helpful? 0
  • +
  • -

#32 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Ant farm Program

Posted 07 June 2012 - 01:30 PM

Should I change it to IAntBase? I'm trying to figure this out.
Was This Post Helpful? 0
  • +
  • -

#33 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 07 June 2012 - 01:32 PM

You don't need it at all.
Was This Post Helpful? 0
  • +
  • -

#34 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7578
  • View blog
  • Posts: 12,749
  • Joined: 19-March 11

Re: Ant farm Program

Posted 07 June 2012 - 01:38 PM

View Postferguson32, on 07 June 2012 - 03:26 PM, said:

Like this Jon?

class AntBase implements IAntBase
{
	private int strength;
	private AntBase ant;
	Random number = new Random();
	
	public AntBase(AntBase ant)
	{
		this.ant = ant;
		strength = number.nextInt(10);
		
	}


If the ants are marching in a line, maybe. :)

What you have here is an ant that points to another ant somewhere. Each ant has an ant that it refers to. There's a very useful data structure that works this way, called a linked list. In a linked list, each element points to the next element, allowing you to scan a list one piece at a time - think of a daisy chain, where each link is tied to a next link. This is a very useful structure, as I say, but I don't see how it relates to this particular case.

As Ryan says,

Quote

Why do you want an instance of AntBase in AntBase?



What use would this serve? Each ant has certain characteristics. One of them, it seems, is "strength". What purpose would this.ant serve in your program?

This post has been edited by jon.kiparsky: 07 June 2012 - 01:38 PM

Was This Post Helpful? 1
  • +
  • -

#35 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Ant farm Program

Posted 07 June 2012 - 01:42 PM

Could you possibly show me the correct way to set up my constructor if it is to create an ant and give it a random strength. I'll just hound my professor to help with the rest of the program. I don't want to waste any more of everyone's time.
Was This Post Helpful? 0
  • +
  • -

#36 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Ant farm Program

Posted 07 June 2012 - 01:43 PM

View PostRyano121, on 07 June 2012 - 03:13 PM, said:

For now -

strength = randomNumberGenerator.nextInt(10);


Job done.

We already have. Multiple times over, with now 3 pages worth of explanation. Please read our posts.
Was This Post Helpful? 2
  • +
  • -

#37 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Ant farm Program

Posted 07 June 2012 - 01:45 PM

It says to create an ant in the constructor but I'll just do what you guys are saying.
Was This Post Helpful? 0
  • +
  • -

#38 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 07 June 2012 - 01:46 PM

As Jon said before all you need to do in your constructor is initialize your fields. In this particular case the only one we have at the moment is 'strength'.

You assign a random number to the field (which you have already done), and that's it for now. There isn't any other fancy shenanigans that you need to include.

public AntBase()
{
   strength = randomNumberGenerator.nextInt(10);
}


P.S - I think you should rename your AntBase class to just Ant (as it's meant to be). I reckon that may clear things up slightly.

This post has been edited by Ryano121: 07 June 2012 - 01:47 PM

Was This Post Helpful? 2
  • +
  • -

#39 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8964
  • View blog
  • Posts: 33,624
  • Joined: 12-June 08

Re: Ant farm Program

Posted 07 June 2012 - 01:48 PM

Quote

It says to create an ant in the constructor but I'll just do what you guys are saying.


...

Quote

Constructor The constructor should create an ant and give it strength.

*sigh* No.. it says to create an ant which is an object... and the object itself is created by it's own constructor.... nothing there says "the object's constructor should create another object instance inside the scope of the constructor".

Nothing.
Was This Post Helpful? 1
  • +
  • -

#40 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: Ant farm Program

Posted 07 June 2012 - 01:58 PM

Ok one last thing. I really don't know how to do the fight and breed methods. I'm not gunna ask you guys to walk me through it since i know you're probably already sick of me :sad3: Do you know of a tutorial that I could relate to it that could help me figure it out?
Was This Post Helpful? 0
  • +
  • -

#41 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 07 June 2012 - 02:05 PM

I'll give you little hint.

public IAntBase breed(IAntBase mate)


So basically what want to do is get the strength of the mate, and the strength of the current ant, find the largest one and create a new Ant with that strength.

int mateStrength = mate.getStrength(); // get the strength of the mate
int myStrength = this.getStrength(); // get my strength

// now work out the largest number or average the two


Once we have the new strength that the baby ant will have, we can create a new instance an return it.

If you don't feel like making a couple of if statements to get the greatest number, you can use the Math.max(int a, int b ) method, which will return the greater of the two parameters.


As for the fight method, the concept is much the same

boolean fight(IAntBase competitor)


We get the strength of the competitor and strength of the current ant -

int competitorStrength = competitor.getStrength(); // get the strength of the competitor
int myStrength = this.getStrength(); // get my strength


Then if the competitors strength is greater than my strength, he has killed me so we return false. Otherwise if we have a greater strength than the competitor then we kill that other puny ant - so we return true.

Have a go at them and ask back here with your problems.

This post has been edited by Ryano121: 07 June 2012 - 02:12 PM

Was This Post Helpful? 2
  • +
  • -

#42 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Ant farm Program

Posted 07 June 2012 - 02:06 PM

The fight() method is modeled after the Comparable interface. Read up on how Object comparison works and model your fight() method after that.
Was This Post Helpful? 0
  • +
  • -

#43 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Ant farm Program

Posted 07 June 2012 - 07:17 PM

Reading this thread made me cry a little... but then I thought about SimAnt and felt happy again.

Laser spider goes pew pew pew!

This post has been edited by atraub: 08 June 2012 - 08:02 AM

Was This Post Helpful? 2
  • +
  • -

#44 Sothatsit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 08-June 12

Re: Ant farm Program

Posted 08 June 2012 - 05:10 PM

in your class have something like this

class AntBase implements IAntBase
{
        int Strength

	public AntBase()
	{
		Strength = Random.nextInt(10);
		
	}
	public IAntBase breed(IAntBase mate)
	{
		
	}
	public boolean fight(IAntBase enemy)
	{
		
	}
	public int getStrength()
	{
		return Strength;
	}
}




as you can see before the constructor i have added a variable (int Strength) and in the constructor i have put "Strength = Random.nextInt(10)" this will assign the variable Strength with a random number from 0-10 you can change the 10 to whatever number you want and that will become the maximum number when using random.

View PostSothatsit, on 08 June 2012 - 05:09 PM, said:

in your class have something like this

class AntBase implements IAntBase
{
        int Strength

	public AntBase()
	{
		Strength = Random.nextInt(10);
		
	}
	public IAntBase breed(IAntBase mate)
	{
		
	}
	public boolean fight(IAntBase enemy)
	{
		
	}
	public int getStrength()
	{
		return Strength;
	}
}




as you can see before the constructor i have added a variable (int Strength) and in the constructor i have put "Strength = Random.nextInt(10)" this will assign the variable Strength with a random number from 0-10 you can change the 10 to whatever number you want and that will become the maximum number when using random.



sorry im new didn't know there where extra pages :P
Was This Post Helpful? 1
  • +
  • -

#45 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: Ant farm Program

Posted 08 June 2012 - 05:34 PM

@Sothatsit: Don't forget to test your code first. Remember you need a semi-colon after int Strength, and that the Random nextInt() method is non-static, so it must be invoked on a Random object, not the Random class. :)
Was This Post Helpful? 2
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5