Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-05-2007, 04:44 AM
Member
 
Join Date: Nov 2007
Posts: 7
mispeed is on a distinguished road
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!!!??
//Thanks again
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-05-2007, 09:17 AM
Member
 
Join Date: Jun 2007
Location: Colombo, Sri Lanka
Posts: 31
hiranya is on a distinguished road
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.

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.
Code:
private Cell[] cellArray = new Cell[numCells];
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-05-2007, 10:33 AM
Member
 
Join Date: Nov 2007
Posts: 7
mispeed is on a distinguished road
yea sorry if it was unclear...
i'll try to explain!

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!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-05-2007, 12:14 PM
Member
 
Join Date: Jun 2007
Location: Colombo, Sri Lanka
Posts: 31
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-05-2007, 08:24 PM
Member
 
Join Date: Nov 2007
Posts: 7
mispeed is on a distinguished road
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!

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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-05-2007, 08:38 PM
Member
 
Join Date: Nov 2007
Posts: 7
mispeed is on a distinguished road
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-------
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-05-2007, 08:40 PM
Member
 
Join Date: Nov 2007
Posts: 7
mispeed is on a distinguished road
bad typing sorry

where it says Cell thats the beginin of the second class! sorry about that!!!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-06-2007, 07:47 AM
Member
 
Join Date: Jun 2007
Location: Colombo, Sri Lanka
Posts: 31
hiranya is on a distinguished road
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.
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.
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.

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

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-08-2007, 05:52 AM
Member
 
Join Date: Nov 2007
Posts: 7
mispeed is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-08-2007, 05:53 AM
Member
 
Join Date: Nov 2007
Posts: 7
mispeed is on a distinguished road
........ ........
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array Help bluegreen7hi New To Java 2 03-28-2008 04:25 AM
can anyone help... 2d Array Mark1989 New To Java 2 03-12-2008 10:59 PM
Would appreciate your help with 2d Array.. cloudkicker New To Java 1 02-11-2008 04:34 PM
Data array to file Java Tip Java Tips 0 01-16-2008 12:42 PM
Add data to an array adlb1300 New To Java 8 11-05-2007 04:01 AM


All times are GMT +3. The time now is 11:22 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org