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.
IndexProgramming Questions & HelpSyntax Questions › beginner question re: classes
Page Index Toggle Pages: 1
beginner question re: classes (Read 331 times)
beginner question re: classes
Oct 29th, 2008, 7:20pm
 
hi y'all, complete novice here...i'm just now learning how to define classes and have a simple question. when creating a class that won't have any external variable input (all variables will be set by random() within each object), do i need to declare these internal variables at the onset of class creation, or can i simply declare them within the methods in which they'll be used? my brain is telling me i can do this:

class NewThing{
 NewThing(){
 }
 void method1(){
   float method1Var1 = 0;
   float method1Var2 = 0;
 }
}

or does it need to look more like this?:

Class NewThing{
 float method1Var1;
 float method1Var2 ;

 NewThing(){
   }
 void method1(){
   method1Var1 = 0;
   method1Var2 = 0;
 }
}

if it is as simple as option number one, can i skip the constructor as well since i'm not passing any variables from outside the object? hope that makes sense keeping in mind i'm such a noob (two weeks ago i didn't know what a semi-colon was for) that they could both be wrong...thanks!
Re: beginner question classes
Reply #1 - Oct 29th, 2008, 9:02pm
 
It is difficult to judge from your mock sample... Smiley

The second form is useful if you need to persist the values for the life of the object. That's what classes are made for: to associate data (the class fields/variables) and functionality (the methods).

In the first form, the variables will live only the time span of the method call.

In fact, in most classes, you often mix both: you have fields which model the object and you create variables within functions to do intermediate computing.

Now, you can have a class made only of static functions (not relying on class' fields), like Java's Math one: it is more a way to group together (and provide a namespace for) a bunch of separate but related pure functions...

Note that you don't need to declare a constructor if it will remain empty, Java provides a default constructor if not present.

[EDIT] Indeed I inverted "first" and "second", corrected to avoid further confusion... :-P
Re: beginner question classes
Reply #2 - Oct 29th, 2008, 10:04pm
 
i think you've answered my question...which put in simpler terms would be: "is my first sample okay?" and the answer seems to be 'yes'.

the fact that you could understand my post at all means i'm making progress...like i said, i haven't programmed a line my entire life, so even the lingo takes some getting used to. thanks!!!
Re: beginner question classes
Reply #3 - Oct 29th, 2008, 11:37pm
 
i think philho has that the wrong way around - in the top example the variables are local to method1 and won't be available outside it whereas in the second case they are members of the class and would be available in method1 AND any other methods you add to the same class. (and, using accessor methods, to other classes)

first is fine, preferable even, if you only want the variables to be used within method1 (the general idea is to limit the scope of the variable as much as possible to keep things tidy)
Re: beginner question classes
Reply #4 - Oct 29th, 2008, 11:58pm
 
koogy wrote on Oct 29th, 2008, 11:37pm:
first is fine, preferable even, if you only want the variables to be used within method1 (the general idea is to limit the scope of the variable as much as possible to keep things tidy)


sounds good to me! thanks to you both.
Re: beginner question classes
Reply #5 - Oct 30th, 2008, 6:49pm
 
You are right, koogs, thanks for pointing that out, I corrected my message to avoid confusion...
Re: beginner question classes
Reply #6 - Oct 31st, 2008, 2:11am
 
this is what i ended up with...

int numInsects = 20;
Insect[] insects;

void setup(){
 noStroke();
 background(30);
 size(640,360);
 frameRate(120);
 smooth();
 insects = new Insect[numInsects];
 for (int i = 0; i < numInsects; i++){
   insects[i] = new Insect();
 }
}

void draw(){
 fill(0,1);
 rect(0,0,width,height);
   for (int i = 0; i < numInsects; i++){
     insects[i].display();
   }
}

class Insect{
 float x = 320;
 float y = 180;
 float diam;
 float angle = 0;
 float mult1 = random(-1.5, 1.5);
 float mult2 = random(-1.5, 1.5);

 void display (){
   x += sin(angle/mult1);
   y += cos(angle/mult2);
   diam = noise(angle) * 15;
   if (diam > 20 || diam < -20){
     diam = 1.01;
   }
   angle += .01;
   ellipseMode(CENTER);
   fill(20,50,100,10);
   ellipse (x, y, diam*3.5, diam*3.5);
   fill(200, 250, 200, 100);
   ellipse (x, y, diam/1.2, diam/1.2);
   noFill();
   stroke(200, 255, 255, 10);
   ellipse(x, y, diam*3.5, diam*3.5);
   noStroke();    
 }
}

my first use of classes and arrays. its got its problems but at least its nice to look at. thanks again!
Re: beginner question classes
Reply #7 - Oct 31st, 2008, 1:00pm
 
that does look nice.

maybe try colour-cycling the insects or respawning them from time to time.
Page Index Toggle Pages: 1