How to create multiple object

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?

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();}
  }

    

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

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

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();}
  }

    
2 Likes

Get rid of this please …??

1 Like

sorry I did not clean my code yet
done :smiley:

1 Like

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.

1 Like