Processing Subclass and parameter question

edited November 2013 in Questions about Code

Task 1. Jumping things. This sketch is supposed to work so that draw a thing (rectangle) somewhere in the window, and every time you click the mouse will skip this rectangle to a new location. The size of the rectangle is determined by the parameter to the constructor.

cus i cant get my code to run, do you know how to fix that? does my code and task 1 match?

this is the main file!

Hoppeting ht;
void setup() {
size(600, 400);
ht = new Hoppeting(30);
}
void draw() {
background(0);
ht.display();
}


void mouseClicked() {
ht.hopp();
}

this is the class file:

class Hoppeting {

  // Variabler

  float x;
  float y;
  float bredde;
  float hoyde;



  //Constructor

  Hoppeting() {

    x = random(height);
    y = random(width);
    hoyde = 50;
    bredde = 100;

  }


  //Function

  void display() {

    background(255, 0, 0);
    rect(x, y, bredde, hoyde);
  }

  void hopp() {


        x = random(width); 
        y = random(height);

  }
}

.. in the main file at the line:

ht = new Hoppeting(30);

what does the (30) mean?

task 2.

To create a Jumping Circle, a subclass of jumping thing that draws a circle instead of a square. write complete and comment code.

here is my class & subclass, can someone tell me if i do it right? cus i dont get it to work...

class Hoppeting {

  // Variabler

  float x;
  float y;
  float bredde;
  float hoyde;



  //Constructor

  Hoppeting(int hoppetingLgth) {

    x = random(height);
    y = random(width);
    hoyde = 50;
    bredde = 100;
  }


  //Function

  void display() {

    background(255, 0, 0);
    rect(x, y, bredde, hoyde);
  }

  void hopp() {


    x = random(width); 
    y = random(height);
  }
}




class Hoppesirkel extends Hoppeting {

  // Variabler

  float x;
  float y;
  float bredde;
  float hoyde;



  //Constructor

  Hoppeting(int hoppeting) {

    x = random(height);
    y = random(width);
    hoyde = 50;
    bredde = 100;
  }


  //Function

  void display() {

    background(255, 0, 0);
    ellipse(x, y, bredde, hoyde);
  }

  void hopp() {


    x = random(width); 
    y = random(height);
  }
}

task3.

Describe in your own words what you need to change the example above to use the class jump circle instead of jumping stuff class

task 4.

using an array list should make it possible to have multiple instances of both jumping thing and jump circle in the sketch. write complete and comment code for the changes you choose to do.

thanks!

Answers

  • edited November 2013

    i would be really gratefull if someone could help me! cus i got exam in this, and i cant find any good tutorial :(

  • edited November 2013

    what does the (30) mean?

    It's a parameter value to be sent to the class' constructor.
    However, your present constructor @ task 1 doesn't accept any parameters!

  • edited November 2013

    @gotoloop how can i fix and make it right? i dont really understand the parameter value... and what it do.. can you explain me?

  • edited November 2013

    It's hard to spot what're exactly your questions. You should be more specific! #-o

  • @gotoloop

    sorry my english is bad....

    The thing is that i dont understand how to fix my constructor so it accept parameters..

    any what parameters do..

  • edited November 2013

    Maybe a simpler example can enlighten you up a bit?: :-?

    class MyClass {
      int myField;
    
      MyClass(int myParameter) {
        myField = myParameter;
      }
    }
    
  • @gotoloop still dont understand...

    can you do a example with my code? How should my code be, so it can be right?

  • Here you go son, i think i fixed it for you now.

    Main

    Hoppeting ht;
    Hoppesirkel hs;
    
    
    void setup() {
      size(600, 400);
      hs = new Hoppesirkel(30);
      ht = new Hoppeting(30);
    }
    void draw() {
      background(0);
      hs.display(); 
      ht.display(); 
    
    }
    
    
    void mouseClicked() {
      ht.hopp();
      hs.hopp();
    }
    

    ** Superclass**

    class Hoppeting {
    
      // Variabler
    
    float x, y, bredde, hoyde;
    
    
      //Constructor
    
      Hoppeting(int hoppeting) {
    
        x = random(height);
        y = random(width);
        hoyde = 50;
        bredde = 100;
      }
    
    
      //Function
    
      void display() {
    
        background(255, 0, 0);
        rect(x, y, bredde, hoyde);
      }
    
      void hopp() {
    
        x = random(width); 
        y = random(height);
      }
    }
    

    sub-class

        class Hoppesirkel extends Hoppeting {
    
          // Variabler
        Hoppesirkel(int h) {
    
        // constructor
         super(h);
    
          }
    
    
          //Function
    
          void display() {
    
            background(255, 0, 0);
            ellipse(x, y, bredde, hoyde);
          }
    
          void hopp() {
    
    
            x = random(width); 
            y = random(height);
          }
        }
    
Sign In or Register to comment.