10.2 – Many objects, algorithms

On Tuesday we learned how to make write a class and make an object:

Car myCar; //Declare a Car object

void setup() {
  size(200, 200); 
  myCar = new Car(); //initialize the car object-- this calls your class' constructor
}

void draw() {
  background(0);
  myCar.display();
  myCar.drive();
  println(myCar.xpos);
}
class Car{ //note the Capital C, and lack of ()

//Class's data, just like global variables at the top of your sketch
color c;
int xpos;
int ypos;
int xpseed;

Car(){ //The Constructor, same name as my class, works just like setup
c = color(175);
 xpos = width/2;
 ypos = height/2;
 xspeed = 1;
}

void display(){ //Methods, same old functions!
//display my car
fill(c);
rect(xpos, ypos, 20, 20);
}

void moveCar(){
xpos = xpos + speed;
}

} //the closing bracket for my whole class

But the real power of objects is lost on us until we start making more than one of them. We can use this very easily by repeating the process of making an object, but naming the object “variables” differently:

Car myCar1; //Declare the first Car object
Car myCar2; //Declare the second Car object

void setup() {
  size(200, 200); 
  myCar1 = new Car(); //initialize the first car object
  myCar2 = new Car(); //initialize the first car object
}

void draw() {
  background(0);
  myCar1.display(); //display the first car object
  myCar1.drive(); //drive the first car object
  myCar2.display(); //display the second car object
  myCar2.drive(); //drive the second car object
}

Unfortunately, much like when we learned about functions, we won’t be able to tell that there are two car objects because they will have the exact same properties. They both start out at width/2, height/2, are gray in color, and drive at the same speed.

If we think back to the idea of objects, they all have the same data, but ideally they’ll have their own specific traits. All humans have an age, but not the same age. All humans have a gender, but not the same gender.  Similary, all objects have the same data variables, but we can get specific about the values of those variables.

Think back to when we learned how to pass parameters into functions:

void drawBlackBall(int diameter){
fill(0);
ellipse(100, 100, diameter, diameter);
}

Instead of just saying, “make a new car”, we’re going to say, “make a blue car at a location of 10, 50 and a speed of 5. We can do the same thing with our class’s constructor, giving each of our objects their own, specific set of instructions on how they should be made.

Car myCar = new Car(color(0, 0, 255), 10, 50, 5);

The constructor must be rewritten to incorporate these arguments:

Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
      c = tempC;
      xpos = tempXpos;
      ypos = tempYpos;
      xspeed = tempXspeed;
    }

Yes, this looks really reptitive and confusing. You declare your object’s instance variables at the top of your class, then you include these weird temp variables in the constructor, that you then set equal to your instance variables? Huh? It’s ok if this feels weird, we’re going to practice it until it starts to make sense.

Let’s review parameter passing–

(Learning Processing)

We can’t call the parameters inside of our constructor by the same name as our instance variables because they would re-write the “global” instance variables we just created.

Arguments are local variables used inside the body of a function that get filled with values when the function is called. In the examples, they have one purpose only, to initialize the variables inside of an object. These are the variables that count, the car’s actual car, the car’s actual x location, and so on. The constructor’s arguments are just temporary, and exist solely to pass a value from where the object is made into the object itself. ”  – Learning Processing

Putting “temp” in front of your constructor’s temporary variable name is a very common way to keep this idea straight, such as int tempx. You will also see many programmers use an underscore, int _x;  You can name these whatever you want, of course. However, it is advisable to choose a name that makes sense to you, and also to stay consistent. 

Passing parameters into a constructor follows the same rules as when we learned how to pass variables into a function. Now that we defined car to expect arguments, we HAVE to give it the same number and types of arguments when we create the object. If we were to call new Car() without the right number and types of arguments, we would get an error. It is very important that you have the same number of arguments that you define in your constructor and that they follow the same order and are of the same type as your constructor definition.

Let’s look at the same car example as before, but this time lets make multiple objects with unique properties:

Car myCar1;
Car myCar2;

void setup() {
  size(200,200);
//Parameters go inside the object when we create it
  myCar1 = new Car(color(255,0,0),0,100,2);
  myCar2 = new Car(color(0,0,255),0,10,1);
}
void draw() {  
  myCar1.move();
  myCar1.display();
  myCar2.move();
  myCar2.display();
}

/* Even though we're making multiple objects, we only need one class. No matter how many cookies we make, we only need one cookie cutter! */
class Car {
  color c;
  float xpos;
  float ypos;
  float xspeed;

  Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) {
   c = tempC;
   xpos = tempXpos;
   ypos = tempYpos;
   xspeed = tempXspeed;
  }
  void display() {
   stroke(0);
   fill(c);
   rectMode(CENTER);
   rect(xpos,ypos,20,10);
}
  void move() {
   xpos = xpos + xspeed;
   if (xpos > width) {
    xpos = 0; 
   }
  } 
}
Print this page

Leave a Reply

Your email address will not be published. Required fields are marked *