We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
class and array (Read 230 times)
class and array
Feb 16th, 2009, 4:37am
 
Hey all,

I'm trying to make a version of Game of Life Cellular Automata. I want to create a class called cell that would have the x y position, the state (alive or dead), and frequency(how many times it's been alive)

class Cell {
 
 float x;
 float y;
 int state;
 float frequency;

 
 // Make the Cell
 Cell(float tempX, float tempY) {
   x = tempX;
   y = tempY;
   state = 0;

**QUESTION HERE: if I want to create the cell that has these variables, x, y, state and frequency, do I declare them here in Cell(float tempX, float tempY......)?

 // plus functions of finding how many neighbors are on or off
 }

In order to create the collection of cells, should I use the array or arraylist?
Obviously the 2 will have different ways of addressing the cells.

but my major question is... how do I address certain "parameters" of a certain cell.
Say I want to change/update the frequency of cell which is at x=10 y=20 from frequency=0 to frequency=1. How is that done?

Thank you so much!!!

Re: class and array
Reply #1 - Feb 16th, 2009, 9:58am
 
Mmm, a broad question.
First, the array vs. array list question.
The latter is good when you don't know in advance the number of items you will have.
The former is well suited in your case, because usually the cells are in a grid of known dimensions. It is faster, and simpler, so go for it.

I am not sure to understand the first question. If you mean you think to need a constructor with more parameters, the answer is yes, or assign default values and use setters to change these values if needed.

Which addresses the last question: the usual way to change parameters/fields of a class is by using setters:

void setFrequency(float t) { frequency = f; }

for example.
Seems too complex, as you can also just use cellInstance.frequency = f;
That's OK in Processing too, because we have usually small informal scripts.
The advantage of the setters is that you can transform the value before storing it (scale, change unit, etc.), you can update dependent values in the class, etc.
Page Index Toggle Pages: 1