Source Code. Projects. Nerd Stuff. Art Stuff.

Functions, Objects, Constructors

 

A simple calculator that takes day, month and year to determine your age. Makes use of functions and parameter passing.

[gn_spoiler title="Birthday Calculator Source Code:" open="0" style="1"]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void setup(){
 print(getAge(12, 21, 1987));
}

void draw() {

}

int getAge(int day, int month, int year) {
 int age;
 
 // get age based on years
 age = year() - year;
 
 // check to see if birth month and day have passed
 if(month() <= month && day() < day) {
   age = age - 1;
 }
 
 return age;
}
[/gn_spoiler]

 

[gn_spoiler title="Here's the source code: Creates two circles by drawing within Circle class directly." open="0" style="1"]
[gn_tabs style="3"]
[gn_tab title="circleObject"]

1
2
3
4
5
6
7
8
9
10
11
12
13
Circle redCircle;
Circle greenCircle;

void setup() {
  size(640, 480);
  redCircle = new Circle(100, width/2, height/2, color(255, 0, 0));
  greenCircle = new Circle(50, width/4, height/4, color(0, 255, 0));
}

void draw() {
  redCircle.draw();
  greenCircle.draw();
}
[/gn_tab] [gn_tab title="Circle"]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Circle {
 float d;  // diameter
 float x;
 float y;
 color c;
 
 // Constructor
 Circle(float _d, float _x, float _y, color _c) {
   d = _d;
   x = _x;
   y = _y;
   c = _c;
 }
 
 // draw() directly within circle
 void draw(){
   fill(c);
   ellipse(x, y, d, d);  
 }
}
[/gn_tab][/gn_tabs] [/gn_spoiler]