Ant farm Program

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

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

#16 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:33 PM

Can you show me the proper way to setup the constructor so it creates the ant and gives it a strength?
Was This Post Helpful? 0
  • +
  • -

#17 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10570
  • View blog
  • Posts: 39,137
  • Joined: 27-December 08

Re: Ant farm Program

Posted 06 June 2012 - 05:51 PM

No arrays are necessary. Simply invoking randomObject.nextInt(10); will generate a random int from 0-9, where randomObject is the name of the object of type Random.

As for your second question, this line of code: AntBase ant = new AntBase(); is what actually creates the AntBase object. The constructor is invoked on the new AntBase() part of that line. Within the constructor in your AntBase class is where you would assign the variable strength its value:
public AntBase(){
   //assign strength a value
}



Given that you know how to assign a variable, you now need to attempt to take it from here.
Was This Post Helpful? 2
  • +
  • -

#18 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 - 06:38 PM

ok so something like this:

private int strength;
	public AntBase()
	{
		new AntBase();
		strength = randomObject.nextInt(10);
		
	}


I know it's not 100 percent right because it is telling me randomObject cannot be resolved.
Was This Post Helpful? 0
  • +
  • -

#19 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10570
  • View blog
  • Posts: 39,137
  • Joined: 27-December 08

Re: Ant farm Program

Posted 06 June 2012 - 06:40 PM

It's not right for two reasons. I explained in my earlier post why you should not instantiate an AntBase object within the AntBase constructor. I also explained earlier that randomObject was the name I used to represent an Object I instantiated earlier of type Random.
Was This Post Helpful? 1
  • +
  • -

#20 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 - 08:04 PM

Like this:
private int strength;
	Random number = new Random();
	public AntBase()
	{
		new AntBase();
		strength = number.nextInt(10);
		
	}


I thought you were saying to just put new AntBase(); in the constructor so I'm not sure what I need to do to create an ant.

This post has been edited by ferguson32: 06 June 2012 - 08:06 PM

Was This Post Helpful? 0
  • +
  • -

#21 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10570
  • View blog
  • Posts: 39,137
  • Joined: 27-December 08

Re: Ant farm Program

Posted 06 June 2012 - 08:41 PM

This line of code: AntBase ant = new AntBase(); would be used outside of the AntBase class to create an instance of an AntBase object.
Was This Post Helpful? 0
  • +
  • -

#22 ferguson32  Icon User is offline

  • D.I.C Head

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

Re: Ant farm Program

Posted 07 June 2012 - 11:00 AM

Ok so what would go in the constructor for creating an ant? Also is what I did for getting a random strength in the constructor correct?
Was This Post Helpful? 0
  • +
  • -

#23 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Ant farm Program

Posted 07 June 2012 - 11:07 AM

In the constructor you already are creating an Ant. All you need to do is give values to the fields (in this case strength), which you have already done.

And yes the random number generation is good.
Was This Post Helpful? 0
  • +
  • -

#24 ferguson32  Icon User is offline

  • D.I.C Head

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

Re: Ant farm Program

Posted 07 June 2012 - 11:51 AM

Are you saying to just have new AntBase(); in the constructor to create an ant? I don't know if your saying the constructor is all set or not.
Was This Post Helpful? 0
  • +
  • -

#25 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10570
  • View blog
  • Posts: 39,137
  • Joined: 27-December 08

Re: Ant farm Program

Posted 07 June 2012 - 11:54 AM

No. That line does not go in the constructor. I don't mean to be rude, but I've said the same thing half a dozen times. That invokes the constructor. If you put it in your constructor, you will get a StackOverflowError.

Also, we are not here to check over every line of your code and give you the okay. You have a compiler- compile and run your code. That is the best way to see if it works.
Was This Post Helpful? 0
  • +
  • -

#26 ferguson32  Icon User is offline

  • D.I.C Head

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

Re: Ant farm Program

Posted 07 June 2012 - 12:10 PM

ok well ryano said I was creating an ant so I thought that was right. I'm new to this so I don't understanding everything yet. It's been like 10 posts going circles and i still don't know/understand what needs to go in the constructor for creating an ant.
Was This Post Helpful? 0
  • +
  • -

#27 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Ant farm Program

Posted 07 June 2012 - 12:13 PM

For now -

strength = randomNumberGenerator.nextInt(10);


Job done.
Was This Post Helpful? 1
  • +
  • -

#28 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7755
  • View blog
  • Posts: 13,112
  • Joined: 19-March 11

Re: Ant farm Program

Posted 07 June 2012 - 12:18 PM

What goes in the constructor for any object is the code that sets that object up. If there are fields that need to be initialized, this is where you do it.

When you call the constructor, an object is created, and the code in the constructor is run. If that code contains a call to this object's constructor, the compiler will call that constructor. And the code in that constructor will call that constructor, and so forth, until you run out of stack and your code collapses.

Example of a constructor:

public class Book
{
  private String title;
  private String author;

  public Book(String title, String author)
  {
    this.title= title;
    this.author=author;
  }

}

Was This Post Helpful? 2
  • +
  • -

#29 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10570
  • View blog
  • Posts: 39,137
  • Joined: 27-December 08

Re: Ant farm Program

Posted 07 June 2012 - 12:19 PM

Might I suggest as well a few tutorials on class design:
http://www.dreaminco...arallel-arrays/
http://docs.oracle.c...OO/classes.html
http://www.dreaminco...igning-classes/
http://www.dreaminco...ing-classes-ii/
http://www.dreaminco...e-mc2-tutorial/
Was This Post Helpful? 0
  • +
  • -

#30 ferguson32  Icon User is offline

  • D.I.C Head

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

Re: Ant farm Program

Posted 07 June 2012 - 01:26 PM

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);
		
	}

Was This Post Helpful? 0
  • +
  • -

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