Please Explain these for beginners

edited October 2013 in Questions about Code

What is extends? Where, why and how to use it ?

        DrawDot dd1 = new DrawDot(50, 80);
        void setup() { ----
          size(200, 200);
        } 

        void draw() {
          dd1.display();
        } 

        class Dot { 
          int xpos, ypos;
        } 

        class DrawDot extends Dot {
          DrawDot(int x, int y) {
            xpos = x;
            ypos = y;
          }
          void display() {
            ellipse(xpos, ypos, 200, 200);
          }
        }

//-----------------------------------------------

What is "super"? Where, why and how to use this?

      class Poly extends java.awt.Polygon {
        public Poly(float[] x,float[] y, int n) {
          //call the java.awt.Polygon constructor
          super(x, y, n);
        }

        void drawMe() {
          beginShape();
          for (int i = 0; i < npoints; i++) {
            vertex(xpoints[i], ypoints[i]);
          }
          endShape(CLOSE);
        }
      }

//--------------------------------------------------

How does this " for loop" work?

    for (ParticleSystem ps: systems) {
        ps.run();
        ps.addParticle();
      }

//-------------------------------------------------

What is "this"? why do we use it?

    class Ground {
      float x1, y1, x2, y2;  
      float x, y, len, rot;

      // Constructor
      Ground(float x1, float y1, float x2, float y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        x = (x1+x2)/2;
        y = (y1+y2)/2;
        len = dist(x1, y1, x2, y2);
        rot = atan2((y2-y1), (x2-x1));
      }
    }

Answers

  • edited October 2013 Answer ✓

    Sounds like you may need an intro to (java) programming course :)

    Here's my brief answers, for more detail, you'll need to do some googling. maybe start here too: http://wiki.processing.org/w/Main_Page dan shiffman has a great intro to programming video series that I strongly recommend: http://icm.shiffman.net/0.0/

    extends is used when you are making a class and you want to use another class as a sort of template. When you extend a class you can use all the methods and variables from that class as if they were inside your new class. It prevents you from needing to copy all the methods and code from the other class into your new class.

    super is used in conjunction with extends to call the initializer method from the class you are extending from. The root class may set up lots of variables and things you may need in your sub class, this allows you to leverage the setup process of that class.

    that for loop assumes that 'systems' is an array of ParticleSystem objects. It iterates over every object in that array and stores it in the temporary variable 'ps' which then you can use in any way you like.

    'this' is a keyword relating to the variables of that class. for example, in the initialization function, it accepts a variable x1, but the class also has a variable x1. the 'this' keyword tells it when you are talking about the class' x1 vs the method's x1

    It is confusing stuff, but really only the loop applies to basic programming, and there are other ways to do a loop if you are more comfortable, that is just shorthand.

    I would say if you are just starting out, you can work without knowing any of these four things for quite a while, until you start dealing with writing your own classes.

    Hope this helps! ak

  • edited October 2013

    wao ! akiersky :) thanks a lot

    for loop has been explained well. =D> =D> =D> thanks for the glimpse of the concept of "extends" and "super" but I need to see some more explanations.

    thanks again :) :) :) :)

  • edited October 2013 Answer ✓

    There's a Java (and others) crash video course in this site below: :-h
    https://www.wibit.net/?view=course&courseID=6

    And 1 just for OOP concepts:
    https://www.wibit.net/?view=course&courseID=3

  • edited October 2013

    Thanks GoToLoop ..... thanks a lot :D :D

    Can you guys explain Map & HashMap ? I know I can get the information from these links but your experiences is over the top on these and may have refined and simple explanation? :-\" :-\"

  • A Map (HashMap being one way to implement it) just associates a key (any object) to a value (any object too). So you can find back a value by giving its key.

    Keys are often just strings, but can be something else. Values are all kind of objects.

    Of course, in general, we use always the same kind of key and of values in a given hash map, which is specified by giving their types in angle brackets:

    HashMap<String, String> hm = new HashMap<String, String>();
    
    hm.put("1", "One");
    hm.put("2", "Two");
    
    println(hm.get("1"));
    println(hm.get("2"));
    
    // Only one value per key:
    
    hm.put("1", "Un");
    hm.put("2", "Deux");
    
    println(hm.get("2"));
    println(hm.get("1"));
    
  • edited October 2013 Answer ✓

    Processing got a somewhat more simplified implementation of a HashMap<String, String>, called StringDict: :-bd
    processing.org/reference/StringDict.html

  • edited October 2013

    @PhiLho @GoToLoop @akiersky ..... can any one of you tell me how to make use of hasMap, Map or ArrayList whatever is suitable to make thisthread for multiple user. Please help me !

    Since I don't have the advance knowledge of these so I am not able to do but I am learning and I'll try.

  • edited October 2013 Answer ✓

    In composite structures like:

    • Array
    • ArrayList
    • IntList
    • FloatList
    • StringList

    They use a contiguous integer index, varying from 0 to n elements.

    Now, if you may need a non-contiguous index, or it's w/ negative or fractional values.
    Or even String words and classes as index! @-)
    That's when you're gonna need a more complex structure like these 1s:

    • HashMap
    • IntDict
    • FloatDict
    • StringDict

    Now, index is called key instead. And we refer to the whole structure as the <key, value> pair.

    There are even more specialized structures in Processing's reference page:
    processing.org/reference

    Like these 1s below:

    • Table
    • XML
    • JSONArray
    • etc.

    Take a quick look there and you'll probably find 1 more adequate to your needs! =:)

Sign In or Register to comment.