How to make the equivalent of a struct? Do I use classes?

edited February 2014 in Programming Questions

Hello,

I'm new to processing (started learning it yesterday), so I apologize for the simple questions. Any how, I need to make a data structure of sorts. To better explain, let me give you an example:

   1   True   3.3   2.2 
   2   True   2.5   1.1
   3   False  2.2   3.2
   4   True   2.3   6.9

Now, I want to create an array that holds all this information and be able to access it. So, if I were to get 3's information, I'd get - False, 2.2, 3.2.

I'm sure it's quite simple, but I've watched YouTube videos and read references about classes, but they seem a bit more complex than what I want to do. Or is using classes sloppy? What is the recommended method?

Also, I want to make it expandable since I don't know how many times this will be entered - do I use an ArrayList? If so, how do I read and write to it?

Thank you!

Tien

Tagged:

Answers

  • edited February 2014

    there is a good tutorial about classes / oop

    http://processing.org/tutorials/objects/

    this is what you need. [New Version]

    It should be all quite clear.

    In a class you collect the properties (your information e.g. - False, 2.2, 3.2.) and the methods (print it, draw it whatever)

    The class goes from { down to the very last }

    The class is then used like a type (like int or boolean e.g.)

    in the 1st line we make our ArrayList of type Ellipse

    in setup we

    • first make a new Ellipse ellipseTest using new

    • and we push this Ellipse into the ArrayList with add

    The word "new" ist interesting. The class is the concept (the abstract idea/ cookie maker) of an ellipse and only with "new" we make an existing thing from it (the thing / the cookie) with real color and position.

    in draw()

    • we read it with get from the ArrayList

    • and draw it

      ArrayList <Ellipse>  ellipses  = new ArrayList();  
      
      void setup() {
        size(600, 600);
      
        //circle
        int circleSize = 1;
      
        // we add a lot of ellipses
        for (int i=1;i<32;i++) {
          Ellipse ellipseTest = new Ellipse(width/2, height/2, circleSize, color( 220, 0, 0 ));
          ellipses.add (ellipseTest);
          circleSize +=11;
        } // for 
      
        // we add one ellipse more 
        Ellipse ellipseTest = new Ellipse(30, 30, 44, color( 2, 2, 222.0 ));
        ellipses.add (ellipseTest);
      } // func
      
      void draw() {
        // we show them 
        Ellipse ellipseCurrent;
        for (int i=1;i<ellipses.size();i++) {
          ellipseCurrent = ellipses.get(i);
          ellipseCurrent.draw();
        } // for
      } // func 
      
      // ============
      class Ellipse {
      
        float x;
        float y;
        float size;
        color col;
      
        Ellipse(float x, float y, float size, color col) {
          this.x=x;
          this.y=y;
          this.size=size;
          this.col=col;
        }
      
        void draw() {
          stroke(col);
          noFill();   
          ellipse(x, y, size, size);
        }
      } // class
      // 
      
  • @Chrisir the link is missing : )

  • Answer ✓

    In OOP the equivalent of a struct is a class with all members public so for your data

    class MyStruct {
      public boolean b;
      public float n1;
      public float n2;
    
      public MyStruct(boolean b, float n1, float n2) {
        this.b = b;
        this.n1 = n1;
        this.n2 = n2;
      }
    
    }
    

    and to create your array list

    ArrayList<MyStruct> list = new ArrayList<MyStruct>();
    list.add(new MyStruct(true, 3.3, 2.2));
    list.add(new MyStruct(true, 2.5, 1.1));
    list.add(new MyStruct(false, 2.2, 3.2));
    list.add(new MyStruct(true, 2.3, 6.9));
    

    and to access the data

    println(list.get(1).n1); // 2.5
    println(list.get(2).b); // false
    list.get(2).b = true;
    println(list.get(2).b); // true
    
  • Answer ✓

    Oh, @quark beaten me 1st! Anyways, here's my example too: ~:>

    // forum.processing.org/two/discussion/3164/
    // how-to-make-the-equivalent-of-a-struct-do-i-use-classes
    
    final ArrayList<Struct> structs = new ArrayList();
    
    void setup() {
      structs.add( new Struct(true, 3.3, 2.2) );
      structs.add( new Struct(true, 2.5, 1.1) );
      structs.add( new Struct(false, 2.2, 3.2) );
      structs.add( new Struct(true, 2.3, 6.9) );
    
      for (Struct struct: structs)  println(struct);
    
      exit();
    }
    
    class Struct {
      boolean b;
      float x, y;
    
      Struct(boolean bool, float val1, float val2) {
        b = bool;
        x = val1;
        y = val2;
      }
    
      String toString() {
        return b + "\t [ " + x + ", " + y + " ]";
      }
    }
    
  • guys, intentionally I gave him something different but similar to his problem

    so he can make the adaptions and learn by himself...

    don't you guys agree with my intention...?

    ;-)

  • @Chrisir your intentions are fine but anyone who thinks that using classes might be sloppy obviously needs an extra hand. :D

  • Absolutely!

    :D

  • edited February 2014

    Hahaha. While I appreciate your intention, I had a project that I needed finished before the night was over. :D

    I'm not a programmer by any means - my boss laughed at the fact that one of the programming languages that I first learned was Fortran... which I learned over twenty years ago. So, for not having programmed since, me completing an actually useful Processing sketch isn't too shabby. :P (Making a printable "binary LP" to be read by infrared sensor array.)

    Thanks, though, to everyone here. Hopefully I'll learn the true use of classes later and redeem myself.

    Tien

Sign In or Register to comment.