View Single Post
  #4 (permalink)  
Old 11-05-2007, 12:14 PM
hiranya hiranya is offline
Member
 
Join Date: Jun 2007
Location: Colombo, Sri Lanka
Posts: 32
hiranya is on a distinguished road
Hi,

I had a look at your code but still the scenario is not very clear to me. I think what you need to do is the following.

You have an array of Cell objects (9 Cells). Each cell has the following two attributes.
1. amount_of_food which is of type int
2. creepers which is an array of Creeper objects

In the first method in the 'one' class you try to put some values in the amount_of_food attribute in each cell. Then in the second method you are trying to put some values in the creepers array.

If this is what you want to do you may do something like this.

Code:
public class Cell { private int amountoffood; private Creeper[] creepers=new Creeper[10];//the array within a cell (I assumed the size to be 10) /* * This method accepts an integer value and * put them in all the creepers in the array */ public void addCreeper(int weight) { for(int index = 0; index < creepers.length; index++) { if(creepers[index] != null) { creepers[index].setAge(1); //Set the age creepers[index].setWeight(weight); //Set the weight } } } public void setAmountofFood(int food) { amountoffood=food; //Set the amount of food } } public class One{ private int birthWeightCreepers = 3; private Cell[] cells = new Cell[9]; public void initializeFood() { for(int i = 0; i < 9; i++) { int randomNum = (int) (Math.random() * 9); //Generate a random number between 0-9 cells[i].setAmountofFood(randomNum); //Assign the random value as the amount of food in a cell } } public void initializeCreepers() { for(int i = 0; i < 9; i++) { cells[i].addCreeper(birthWeightCreepers); //Assign the birth weight to the creepers } } }
Is this anywhere near what you actually want?
If not please feel free to ask for help.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Reply With Quote