Array

edited October 2013 in Questions about Code

How do i put this in an array

Car myCar1;
Car myCar2; // Two objects!
Car myCar3;
Car myCar4;
Car myCar5;
Car myCar6;
Car myCar7;
Car myCar8;
Car myCar9;
Car myCar10;
void setup() {
  size(250,250);
  myCar1 = new Car(color(255,0,0),0,100,2); // Parameters go inside the parentheses when the object is constructed.
  myCar2 = new Car(color(0,0,255),0,10,1);
  myCar3 = new Car(color(25,247,67),0,112,1);
  myCar4 = new Car(color(25,247,231),0,40,5);
  myCar5 = new Car(color(232,247,25),0,65,7);
  myCar6 = new Car(color(232,37,206),0,24,3);
  myCar7 = new Car(color(129,109,73),0,150,2);
  myCar8 = new Car(color(250,96,0),0,175,4);
  myCar9 = new Car(color(26,92,232),0,200,2);
  myCar10 = new Car(color(26,232,147),0,240,6);
}

void draw() {
  background(255);
  myCar1.move();
  myCar1.display();
  myCar2.move();
  myCar2.display();
  myCar3.move();
  myCar3.display();
  myCar4.move();
  myCar4.display();
  myCar5.move();
  myCar5.display();
  myCar6.move();
  myCar6.display();
  myCar7.move();
  myCar7.display();
  myCar8.move();
  myCar8.display();
  myCar9.move();
  myCar9.display();
  myCar10.move();
  myCar10.display();
}
class Car { // Even though there are multiple objects, we still only need one class. No matter how many cookies we make, only one cookie cutter is needed.Isn’t object-oriented programming swell?
  color c;
  float xpos;
  float ypos;
  float xspeed;
  Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { // The Constructor is defined with arguments.
    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;
    }
  }
}

Answers

  • 1st, when posting a code here, highlight it and then hit CTRL+K to format it! :-w
    And the article link below is gonna surely help ya out: :-B

    http://wiki.processing.org/w/From_several_variables_to_arrays

  • I am still unsure about what to do

  • instead of Car car1... Car car10 you can write

    Car[] car = new Car[10];

    then reference them as car[0], car[1], ... car[9]

    which doesn't seem much, until you realise that you can use a variable as the index and a for-loop rather than calling them all individually.

  • NB array indexes start at 0, not one, hence 0-9 rather than 1-10

    this is a better link that the one GotoLoop posted http://www.homeandlearn.co.uk/java/java_arrays.html (and the following page)

  • edited October 2013

    similar to this?

    Car[] car = new Car[10];
    void setup() {
      size(250,250);
      car[0] = new Car(color(255,0,0),0,100,2); // Parameters go inside the parentheses when the object is constructed.
      car[1] = new Car(color(0,0,255),0,10,1);
      car[2] = new Car(color(25,247,67),0,112,1);
      car[3] = new Car(color(25,247,231),0,40,5);
      car[4] = new Car(color(232,247,25),0,65,7);
      car[5] = new Car(color(232,37,206),0,24,3);
      car[6] = new Car(color(129,109,73),0,150,2);
      car[7] = new Car(color(250,96,0),0,175,4);
      car[8]= new Car(color(26,92,232),0,200,2);
      car[9] = new Car(color(26,232,147),0,240,6);
    }
    
    void draw() {
      background(255);
      car[0].move();
      car[0].display();
      car[1].move();
      car[1].display();
      car[2].move();
      car[2].display();
      car[3].move();
      car[3].display();
      car[4].move();
      car[4].display();
      car[5].move();
      car[5].display();
      car[6].move();
      car[6].display();
      car[7].move();
      car[7].display();
      car[8].move();
      car[8].display();
      car[9].move();
      car[9].display();
    }
    class Car { // Even though there are multiple objects, we still only need one class. No matter how many cookies we make, only one cookie cutter is needed.Isn’t object-oriented programming swell?
      color c;
      float xpos;
      float ypos;
      float xspeed;
      Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { // The Constructor is defined with arguments.
        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;
        }
      }
    }
    
  • yeah, but remember what i said about for loops? lines 18-37 are crying out for a for loop...

  •             Car[] myCar = new Car[10];
                void setup() {
                  size(250, 250);
                  for (int i=0;i<myCar.length;i++) {
                    myCar[i] = new Car(color(random(255), random(255), random(255)), i*10+10, i*10+30, (int)random(1, 10));
                  }
                }
    
                void draw() {
                  background(255);
                  for (int i=0;i<myCar.length;i++) {
                    myCar[i].move();
                    myCar[i].display();
                  }
                }
                class Car { 
                  color c;
                  float xpos;
                  float ypos;
                  float xspeed;
                  Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { // The Constructor is defined with arguments.
                    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;
                    }
                  }
                }
    
  • see, when i see something that looks like a homework question, i try to answer in hints rather than just pasting code...

  • edited October 2013

    I am not sure about the error it is giving me.

    Car[] car = new Car[10];
    void setup() {
      size(250,250);
       for (int i; i < 10; i++) {
      new Car[i] = new Car(color(random(255), random(255), random(255)), i*10+10, i*10+30, (int)random(1, 10));
      }
    }
    
    void draw() {
      background(255);
       for (int i; i < 10; i++) {
        new Car[i].move();
        new Car[i].display();
      }
    
    }
    class Car { // Even though there are multiple objects, we still only need one class. No matter how many cookies we make, only one cookie cutter is needed.Isn’t object-oriented programming swell?
      color c;
      float xpos;
      float ypos;
      float xspeed;
      Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { // The Constructor is defined with arguments.
        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;
        }
      }
    }
    
  • Posting unformatted code this way makes help more difficult! :((
    Have you read what I said above on how to format posted code? X(

  • "I am not sure about the error it is giving me."
    Would have helped to give the error message and the line where you get it...

    Fortunately, we can run this code...

    new Car[i] = new Car(...);
    

    makes no sense!

    Should be:

    car[i] = new Car(...);
    

    ie. you must use the array you created.

  • edited October 2013

    Fixed your code: =:)

    // forum.processing.org/two/discussion/612/array
    
    final static int NUM = 10;
    Car[] cars = new Car[NUM];
    
    void setup() {
      size(250, 250);
    
      smooth();
      rectMode(CENTER);
      stroke(0);
      strokeWeight(1.5);
    
      final int gap = (height)/NUM;
    
      for (int i = 0; i != NUM; i++) {
        cars[i] = new Car((color) random(#000000), 
        (int) random(width), i*gap + Car.H, random(1, 10));
      }
    }
    
    void draw() {
      background(-1);
      for (int i = 0; i != NUM; cars[i++].script());
    }
    
    class Car {
      final static int W = 20, H = 10;
      final static int WW = W>>1, HH = H>>1;
    
      float xpos;
      final int ypos;
      final float xspd;
      final color c;
    
      Car(color cc, int x, int y, float s) {
        c = cc;
        xpos = x;
        ypos = y;
        xspd = s;
      }
    
      void move() {
        if ((xpos += xspd) > width - WW)   xpos = -WW;
      }
    
      void display() {
        fill(c);
        rect(xpos, ypos, W, H);
      }
    
      void script() {
        move();
        display();
      }
    }
    
Sign In or Register to comment.