# How to create multiple object

**URL:** <https://discourse.processing.org/t/how-to-create-multiple-object/39504>\
**Category:** Coding Questions\
**Created:** [October 30, 2022, 10:08am UTC](https://discourse.processing.org/t/how-to-create-multiple-object/39504 "2022-10-30T10:08:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tiredperson](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@tiredperson](https://discourse.processing.org/u/tiredperson)\
**Post date:** [October 30, 2022, 10:08am UTC](https://discourse.processing.org/t/how-to-create-multiple-object/39504/1 "2022-10-30T10:08:26Z")

</div>

I am trying to make a program that creates 10 circles and 10 squares that jiggles randomly in the canvas area

my code generates 10 squares and 10 circles, however, they are extremely clustered, ( I’m sure that my usage of “for” loop, in the end, was not good).

I want them to be far from each other, is there a way to create multiple objects from both classes?

```auto
abstract class shape {
  float x,y;
  shape(){
    x=random(width);
    y=random(height);
    println("this is shape");
  }
  void jiggle(){
    float speed =3;
    x+=speed*random(-1,1);
    y+=speed*random(-1,1);
  }
  abstract void display();
}
class circle extends shape{
  float d=30;
  circle (float id ){
    d=id;
    println(" this is circle ");
  }
  void display(){
    ellipse(x,y,d,d);
  }
}
  
class square extends shape{
  float b=30;
  square (float ib ){
    b=ib;
    println(" this is square ");
  }
  void display(){
    square(x,y,b);
  }
}
shape a;
shape b;
void setup(){
  size(800,600);
  
    a = new circle(25);
    b = new square(50);
  }
  void draw(){
    background(192);
    for (int i=0; i<10; i++){
      a.display();
      a.jiggle();
      b.display();
      b.jiggle();}
  }

    

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2022, 10:19am UTC](https://discourse.processing.org/t/how-to-create-multiple-object/39504/2 "2022-10-30T10:19:54Z")

</div>

Almost

Everything would be better if you had 2 arrays, one for the circles and one for the rects

In setup() you need a for loop with a[i]= new …

at the moment you have only 2 items not 20

---

<div class="post-metadata">

**Author:** ![tiredperson](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@tiredperson](https://discourse.processing.org/u/tiredperson)\
**Post date:** [October 30, 2022, 11:26am UTC](https://discourse.processing.org/t/how-to-create-multiple-object/39504/4 "2022-10-30T11:26:07Z")

</div>

thank you bro  
it worked  
I learned 2 new things  
the first one is pretty obvious but for some reason, it did not click in my mind that I can create an array that contains the new class that I created  
the second thing is that the subclass doesn’t inherit the Constructor

```auto
abstract class shape {
  float x,y;
  shape(){
    x=random(width);
    y=random(height);
    println("this is shape");
  }
  void jiggle(){
    float speed =3;
    x+=speed*random(-1,1);
    y+=speed*random(-1,1);

  }
  abstract void display();
}
class circle extends shape{
  circle(){
    x=random(width);
    y=random(height);}
  float d=30;
  circle (float id ){
    d=id;
    println(" this is circle ");
  }
  void display(){
    ellipse(x,y,d,d);
  }
    float rex() {
    return x;}
}
  
class square extends shape{
  float b=30;
  square(){
    x=random(width);
    y=random(height);}
  square (float ib ){
    b=ib;
    println(" this is square ");
  }
  void display(){
    square(x,y,b);
  }
}
circle a;
square b;
shape [] arrc; 
shape [] arrs; 
void setup(){
  size(800,600);
  arrc = new shape [10];
  for ( int i =0;i<arrc.length;i++) {
    arrc[i]=new circle();
  }
  arrs = new shape [10];
  for ( int i =0;i<arrs.length;i++) {
    arrs[i]= new square() ;
    print(arrs[i]);
  }
    a = new circle(25);
    b = new square(50);
  }
  void draw(){
    background(192);
    for ( int i =0;i<arrs.length;i++) {
      arrc[i].display();
      arrc[i].jiggle();
      arrs[i].display();
      arrs[i].jiggle();}
  }

    

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 30, 2022, 11:27am UTC](https://discourse.processing.org/t/how-to-create-multiple-object/39504/5 "2022-10-30T11:27:51Z")

</div>

> [@tiredperson](#):
>
> ```auto
> circle a;
> square b;
> 
> ```

Get rid of this please …??

---

<div class="post-metadata">

**Author:** ![tiredperson](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@tiredperson](https://discourse.processing.org/u/tiredperson)\
**Post date:** [October 30, 2022, 11:30am UTC](https://discourse.processing.org/t/how-to-create-multiple-object/39504/6 "2022-10-30T11:30:51Z")

</div>

sorry I did not clean my code yet  
done 😃

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [October 30, 2022, 6:01pm UTC](https://discourse.processing.org/t/how-to-create-multiple-object/39504/7 "2022-10-30T18:01:06Z")

</div>

One of the points of using a shared base class is that you don’t need to have separate arrays of squares and circles. You can have just one array of the base class type and add to it objects of either subclass.
