Results 1 to 10 of 10
  1. #1
    mispeed is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default add data into an array

    hi everyone,

    here is my problem. thanks for any help in advance!!!:)

    i have an array of cells and initial number of creepers

    private int initialCreeper;//get and set methods for it
    private int[] cellArray = new int[numCells];
    private int numCells;

    private void setNumCells(int cell)
    {
    numCells = 10;
    numCells = cell;
    }
    public int getNumCells
    {
    return numCells;
    }

    //here is where things go wrong
    public void someMethod()
    {
    for(int i = 0; i < initialCreepers; i++)
    {
    int randomCell = (int) (Math.random() * numCells);
    Cell creeper = new Cell();
    cellArray[randomCell] = creeper.addCreeper();//this is a method from a different class where there is //an array of Creepers. The whole point is to add the creeper to the cell and i //know i am not doing it the right way because addCreeper() is a void method. //Not really sure how to do it though!!!?? :confused:
    //Thanks again

  2. #2
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default

    Hi,
    Your problem is not clear enough. We would be able to help if you can post some more specifics about the matter. Also please explain what this addCreeper() method does.

    Java Code:
    Cell creeper = new Cell();
    cellArray[randomCell] = creeper.addCreeper();
    If the adCreeper() is a void method then definitely the above is wrong. I also noticed that cellArray is an array of type int[]. Then you can't add creepers which are of type Cell to this array. You have to declare the cellArray as follows.
    Java Code:
    private Cell[] cellArray = new Cell[numCells];

  3. #3
    mispeed is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default

    yea sorry if it was unclear...
    i'll try to explain!

    Java Code:
    class One
    {
    private int initialFood = 30;
    private int numCreepers = 30;
    private int birthWeightCreepers = 3;
    
    //here i'm not sure which is correct 
    private Cell[] cell = new Cell[9];
    //or: private int[] cell = new int[9];
    
    //get and set methods for food, creepers, birthWeight
    
    //initialize game method
    public void methodName()
    {
      //add food to the cells
      for(i = 0; i < initialFood; i++)
      {
         //find a cell - random NUM between 0 and numberOfCells
         randomNum = (int) (Math.random() * numCells);
         //add food to the cell
         cell[index] = randomNum; // not sure what would happen when index goes   //past 9, but i couldn't think of anything else:confused:  
    }
    public void methodName2()
    {
      //add creepers to the cells
      for(index = 0; index < numCreepers; i++)
      {
         //random num: 0 and numCells
         randomNum = (int) (Math.random() * numCells);
         //tell the cell to add a Creeper(pass in the birth weight)
             //yea here i'm confused because addCreeper is a method in Cell class ,              
             //so then the cell[] should be of type Cell
             //i think to access the method!
      }
    }
    }//end class
    
    class Cell
    {
      private amount of food;
      private array of creepers//the array within a cell 
    
      //add a creeper method
      public void addCreeper
      {
         //for each element in the array of creepers
         for(index = 0; index < creepers.length; index++)
         {
            //if not null
            if(creepers[index] != null)
            {
               //create a creeper
               Creeper theCreeper = new Creeper();//Creeper has get and set //methods for age and weight and thats all
               //set birth weight and age(age = 1; weight given by caller)
               theCreeper.setAge(1);//set age
     
               //update the element with a ref. to the Creeper
            } //end if  
         }//end for
       }
      }
    }//this is getting too long sorry about that! i think it would give you a better //idea of what's happening!

  4. #4
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default

    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.

    Java 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.

  5. #5
    mispeed is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default

    hi, yea i was a little confused about the amountFood in the Cell class myself, so here is where in the pseudo code it is being used!

    Java Code:
    //class Cell
    //feed the creeper method
    public void feedCreeper()
    {
      //for each creeper
      for(i = 0; i < creepers.lenght; i++)
      {
         //check for food
            //if there is food in the cell :i'm guessing amount of food is just a var. 
            //to store any changes to initialFood from Class one
            Universe amountOfFood = new Universe();
            amountOfFood.getInitialFood();
            if(amountOfFood.getInitialFood() != 0)
            {
               //add to weight and decrement food
             }//end if
                 if(//weight is below minWeight(an instance var. in Class one))
                 {     //set to null
                  end if }
                        //check if overweight and split creeper if so)
                        if(weight > maxWeight)
                        { //set weight to birthWeight, Create new Creeper(set its
                           //birthWeight and its age)
                          }//end if
              }//end for
    }//end feedCreeper  
    //so still not exactly what amount of food is i hope the above helps /n //understand it! initialize process is one method called bigBang from Class One.
    It initializes everything(food, creepers, birthWeight) and find a cell.

  6. #6
    mispeed is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default

    Ha thought of something :)
    classes and methods are: ClassOne(initializes everythin(bigBang)
    updates everything daily(dayliUpdate)
    //feed creepers, update age and weight
    get statistics of a desired cell(getCellStats))
    Cell(get and set for amountOfFood(still confusing)
    add creeper to the cell(addCreeper)
    feed the creature(feedCreeper)
    provide cell statistics(CEllSTATS is a class of its own with get and set for numFood, numCreepers! this method cretes an instance of CellStats class, then sets amountOfFood, count creepers and set it into CellStats)


    Thought this might explain the flow of things-------

  7. #7
    mispeed is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default

    bad typing sorry

    where it says Cell thats the beginin of the second class! sorry about that!!!

  8. #8
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default

    OK..First of all I have to admit that I get the feeling that you have not spent enough time analysing the problem and preparing the design for your application. Most people tend to jump into coding without a proper design in mind and that it is the cause of many problems in my opinion.

    So looking at all posts you have made so far I can give you the following guidelines.

    1. There is definitely a class called 'Creeper'. A creeper has an age and a weight. So the Creeper class should have two integer attributes, namely age and weight. Methods should be provided to get and set these two attributes.
    Java Code:
    public class Creeper {
    	
    	private int weight;
    	private int age;
    	
    	public int getAge() {
    		return age;
    	}
    	public void setAge(int age) {
    		this.age = age;
    	}
    	public int getWeight() {
    		return weight;
    	}
    	public void setWeight(int weight) {
    		this.weight = weight;
    	}
    }
    2. There should be a class called 'Cell'. Each cell has a number of creepers so the Cell class should have an array of Creepers. Note the following line in the pseudo code.
    Java Code:
    //check for food
            //if there is food in the cell :i'm guessing amount of food is just a var. 
            //to store any changes to initialFood from Class one
            Universe amountOfFood = new Universe();
    Clearly amountOfFood is a variable of type 'Universe'. (I hope your program have a class called Universe which probably has an integer attribute called initialFood. The getInitialFood() method simply returns this value. There also should be a setter method because we want to change the value while feeding the creepers) Then there is no need to put the amount of food attribute in the Cell class.

    3. The number of creepers in a cell may vary as new creepers are created while feeding. Then we can't use a simple array in the Cell class to hold creepers. We have to use something like an array list where the number of objects is not limited.

    Java Code:
    import java.util.ArrayList;
    
    public class Cell
    {
      private ArrayList<Creeper> creepers=new ArrayList<Creeper>();//the array within a cell (I assumed the size to be 10) 
    
      /*
       * This method creates a new creeper and adds it to the arraylist
       */
      public void addCreeper(int weight, int age)
      {
    	  Creeper C=new Creeper();
    	  C.setAge(age);
    	  C.setWeight(weight);
    	  creepers.add(C);
      }
      
      public void feedCreepers()
      {
    	  Universe U=new Universe();
    	  for (int index=0; index<creepers.size(); index++)
    	  {
    	          int food=U.getInitialFood();
    		  if (food!=0)
    		  {
    			 //Now feed the right amount of food to the creeper (just use the setWeight() method)
    			 //Set the new initialFood value in the Universe class
    			 //Check for maximum weight and create a new creeper if needed (call addCreeper() method)
    		  }
    	  }
      }
      
      /*
       * This method accepts a weight value and assigns it as the weight of all the creepers
       */
      public void setWieghts(int weight)
      {
    	  for (int index=0; index<creepers.size(); index++)
    	  {
    		  creepers.get(index).setWeight(weight);
    	  }
      }
      
      /*
       * This method accepts an age value and assigns it as the age of all the creepers
       */
      public void setAges(int age)
      {
    	  for (int index=0; index<creepers.size(); index++)
    	  {
    		  creepers.get(index).setWeight(age);
    	  }
      }
    }
    4. You may use any other methods as required to extend the features of the classes.

    5. You may do something like the following in the One class.
    Java Code:
    public class One{
    	private Cell[] cells = new Cell[9];
    	
    	public void BigBang()
    	{
    		initializeWeights();
    		initializeAges();
    	}
    	
    	/*
    	 * Sets the weight of all the creepers in all the cells
    	 */
    	private void initializeWeights()
    	{
    		for (int i=0; i<cells.length; i++)
    		{
    			int weight= (int) (Math.random() * 9);	//Generate values as you need
    			cells[i].setWieghts(weight);
    		}
    	}
    	
    	/*
    	 * Sets the age of the creepres in all the cells
    	 */
    	private void initializeAges()
    	{
    		for (int i=0; i<cells.length; i++)
    		{
    			int age= (int) (Math.random() * 9);	//Generate values as you nees
    			cells[i].setWieghts(age);
    		}
    	}
    }
    Hope this helps

    Regards,
    Hiranya

  9. #9
    mispeed is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

    Default

    yea thank you for the help! I think i need to spend some time on it myself now!
    Thanks a lot...it is pretty close to what i needed to do!

  10. #10
    mispeed is offline Member
    Join Date
    Nov 2007
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 03:25 AM
  2. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 09:59 PM
  3. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 03:34 PM
  4. Data array to file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 11:42 AM
  5. Add data to an array
    By adlb1300 in forum New To Java
    Replies: 8
    Last Post: 11-05-2007, 03:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •