We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I suffer from dyslexia which causes me a lot of problems whenever I get an assignment at my University. Whenever I get an assignment where I'm bombarded with words like: overloading, parameters, recursion, output, input, methods, types, functions and so on I pretty much go cold, I get confused as to what is what and I begin to flip around my books and slides from lectures to figure out what is what (I apparently confuses them with each other or have a hard time understanding what they really are or what they really do, like say overloading). I feel really stupid about this and when I ask someone for help at my University they're all like "aren't you paying attention in class?" or "that's so easy, we learned that ages ago", yeah I feel pretty mocked about it... it's not very nice... sometimes it makes me consider giving up on University (I don't really feel I can go to anyone with my problems and those few people that are willing to associate with me knows even less than I do)... Anyway, I was hoping that someone here might be willing to help me making a list of "what is what for dummies" that I can use whenever I get an assignment, because right now I'm pretty lost whenever I'm told to do something in wording that I just don't understand =/ I mean... I don't even know what a void really is... I know how to use it but no more than that... like I asked someone if a constructor had to be a void and he was like "no" and to this day I still have no idea why...
Answers
Every function/method gotta return something. So, when we want that to be ignored, we use void!
A constructor is a special method which is only invoked when instantiating a class and always returns that instantiated reference!
Since that always returns the same thing (reference
this
), there's no need to specify it. And Java won't allow us anyways! 8-XOverloading is a programming jargon either for a type of polymorphism or for operator multi-usage.
Operator Overloading
https://en.wikipedia.org/wiki/Operator_overloading
It's when an operator is used for more than 1 purpose. Take for example the + operator:
But look what happens when we make literal 5 into a "5":
Since at least 1 of the operands involved is a String, the operator + concatenates them instead of adding them! X_X
Since the operator + behaves differently depending on its operands,
we say that operator + is overloaded for more than 1 purpose! :P
Now for method overloading, take a look at Wikipedia, which is our every-hour friend! :D
https://en.wikipedia.org/wiki/Function_overloading
I'm still confused about the overload... maybe you could help me solve this exercise?
Given that you have declared a function
void loadAssets(PImage [] cards, String myFile);
which of the following could be considered as overloaded loadAssets() and why?
a) int loadAssets(PImage [] cards, String myFile);
b) void loadAssets();
c) loadAssets(PImage [] cards, String myFile);
d) void loadAssets(loadAssets(),String myFile);
`void loadAssets(PImage[] cards, String myFile) {}`:
a)
int loadAssets(PImage[] cards, String myFile) {}
-> False!Returning data-type isn't enough! Java would still be confused which 1 to use!
b)
void loadAssets() {}
-> True!Quantity & type of arguments are different! In this case, no argument at all! Thus Java knows which 1 to use!
c)
loadAssets(PImage[] cards, String myFile) {}
-> False!Lack of returning value makes it a constructor instead! Moreover, arguments are the same as the 1s in the question!!!
d)
void loadAssets(loadAssets(), String myFile) {}
-> False!Methods can't be used as declared argument! Only data-types + variables!!!
GoToLoop's explanations are good, but sometime explaining things in different ways can help understanding too, so here is my take:
void
as return type (because such type is mandatory in function definitions) to tell a function is returning nothing: it is expected to do something, but not to give any result.Note that in Java, functions are generally called methods, because they are part of a class. And variables defined at class level are called fields. Just jargon...
A method that has a return type (different from void) must always return a value of this type (can be
null
if the type is a class).Beside the Help in this Forum the great Daniel Shiffmann wrote an awesome Book learningprocessing.com which is helping me right now to understand the Basics of Programming and Processing.
He has also made some great Videos about Programming Basics here vimeo.com/channels/introcompmedia
Thanks to GoToLoop & PhiLho for making things clearer! And helps me also a lot!
I know some great guys with dyslexia who went to university, and did great there and finished it well. So don't be discouraged. You can do it, you just need to be confident.
@ GoToLoop Thanks for the reply =) but I'm a little confused... what's a method compared to a function? I keep thinking they're exactly the same thing >_< also what's a data-type?
@ PhiLho Thanks for replying =) So uhm... a method and a function is the same thing? That's confusing because I sometime reads an excuse or such where both things are mentioned... surely there must be some sort of difference?
@ robhak Thanks for the links =)
@ Chrisir Thanks for the vote of confidence, I just get easily discouraged... but I hope that when I finish this mini guide I'm working on it'll help me with this stuff.
Function is the generic name for something which gets n parameters and spits out a result.
When a function belongs to a class, it's called a method. *-:)
Procedures and sub-routines are also simpler function variations from other languages.
@ GoToLoop Oh, thanks! BTW did you mean "gets no parameters"? Or else I don't know what the "n" stands for :S
n is representing number of formal parameters; including none. >-)
this is a function makeAnAddition that gets two ints and returns one int; therefore it says "int makeAnAddition" and not "void makeAnAddition". As you can see, the value that the function returns gets into the variable a, and thus the variable a can be printed.
This is an function (printlnPI) that doesn't return a thing :
but please don't say "this is the void printlnPI" but say "this is the function printlnPI (which returns void)"
void is just what it returns
A function is a machine that gets feeded something (or nothing) and does something and returns seomthing (or doesn't)
For your Glossary (since this is what it is, right?) I wish you good luck!
Did you read the great tutorials (especially the first 5) http://www.processing.org/tutorials/
Also the reference http://www.processing.org/reference/
I recommend :
also clair once compiled a document of the reference with an alphabetical index
http://www.clairdunn.com/reference/complete-ref-alpha.pdf
I also could provide this as a word-document, just send a Personal message to me.
Hello again, sorry about the long reply time! Been kinda lost in my exercises I had to turn in.
I've been working on a guide/list for me where I try to explain (to myself) what is what, you can see it here: https://docs.google.com/document/d/12QyuTY_FT6_WUkPSkCOQ5Mkw1SFrCSGjtaUlrbqAQsA
I would really like some feedback on it (about errors and such) and suggestions, there are also some question-marks here and there, and in the end I've asked two questions.
Thanks a lot for your help thus far, it's much appreciated <3
this looks great!
A few remarks.
Procedure or sub-routine is just another word for a function.
An object is one entity in your program. It is derived from a Class. The class is the abstract idea of the thing and the object is the real thing. Think of objects as cookies and of the class a cookie maker: there are many objects but only one cookie maker. To make the object, you use the constructor. The class makes your program better readable since its properties and methods (word for function within a class) are in one package. Neat.
**Hm, better still: When a function doesn't return something, we put void. Otherwise we put the type of what it returns. ** Return (isn’t the same thing as a return type) - return(); this commands ends a function. When the function returns something (and doesn't return void) here the variable is returned: return result;
Greetings, Chrisir
also about color (a question mark in your document) you can read here. Basically each 8 bits of the 32bits int is representing one of the components of the color in this way: AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB (a = alpha).
this line you have twice:
<= less than or equal to
put for the 2nd occurance:
then >= greater than or equal to
then > greater than (but not equal)
then < less than (but not equal)
Value
Method
must be:
this line is wrong (belogs to paragraph variable):
voids don’t return any values and therefore are not a variable.
Class
must contain a constructor (there are classes that don't show the constructor, it's a implicit constructor)
is a package of properties (variables) and methods (functions). This makes the code much better readable, since it is a strong tool to bundle together what would be otherwise not necessarily be recognized as belonging together. Examples are customer or obstacle in a game with position, size, color etc....
objects are derived from a class
Instance
myCar1 = new Car(Mercedes); <- Car is a class. Mercedes is a parameter for the constructor. myCar1 is now an object from the class. myCar1 is an instance of the class Car.
an object derived from a class is an Instance of that class. It's an concretion from the idea class.
Loops (IP4)
while-loop = unknown amount of time
while-loop = unknown amount of times (how many times you run the loop, e.g. when you display all enemies in a game but don't know how many you have)
for-loop = known amount of time
for-loop = known amount of times (how many times)
Array is a list of variables.
myList [3] = 5;
gives the value 5 to the 3rd element in the list (index is 3)
you can also have an array of objects.
Constructor
A Constructor let's you get an object from a class. The return type of the constructor is an object of its class
Thanks a lot! I've updated the lost (the link is the same as before) and so I've changed it a little and added some new question-marks (because I'm an idiot).
But could one of you explain "this" to me? I just had a lecture about it the other day and I don't quite get it... also what is a keyword?
Thanks! You guys are awesome! <3
A keyword in a programming language is any word that belongs to the syntax.
These words are predefined and part of the language and can (and should) therefore not used as a name for a variable or function.
The word "this" references the current sketch in processing or when you are in a class, this class. Some librarys or functions or constructors require the usage of this, so they know what sketch is calling them.
Greetings, Chrisir
Value - A variable has a name (fixed) and a value (changeable, variable). The value is the content of the variable. - fx: “int whatever = 5” or “whatever(int 5, int 5)” ???
int whatever = 5;
***whatever is the variable, 5 is the value, int is the type. ***
a function could be
void whatever(int a, int b)
you would call it with
whatever(5, 7);
(in the paragraph Function)
well.... yes
an elephant isn't a function either....
express clearer what you want to say:
when reading this line:
void whatever ( int 5 ) {
please don't read "void whatever" but read "function whatever" (with return type void / not returning a value)
So a "keyword" is... uhm... pretty much any code that resembles something? like say... float _x; then _x is the keyword? i'm confused because I've never heard "keyword" used before "this" has been mentioned in my lecture... could you perhaps give me a very simple example an idiot like me can understand?
Java (and most programming languages) have a list of reserved words, that cannot be used as variable name or function name, or whatever entity the language has.
This is to ease the parsing (interpretation) of the code.
In Java, void, while, for, do, interface, class, public, private, static, etc. are keywords, reserved words.
You cannot write:
int do = 5;
it will generate a syntax error.
You can write:
int dO = 5;
because this language is case sensitive.
regarding the void thing, well it's just because i thought for quite a while that void was a function, so i just added that to remind me to stop being stupid... pretty much
but when you write "void whatever" is "function whatever" that makes me think that void is a function :s
oh, thanks! i think i understand what a keyword is now :)
as how to use "this" i'm still a little lost at >_<
Keywords like
int
,float
,boolean
, etc. are primitive data-types. In Java, they're 8 in total.You can consider them as adjectives for variables and also for returning value of methods!
Also, any
class/interface
name can be used as data-type too!However it's an object type, not primitive! And they're not keywords as the primitive 1s! [..]
Keyword
void
is a used to denote a lack of returning value for methods.Even though it seems redundant, Java's syntax demands its usage as a grammar rule of sorts.
Only a constructor (which is a special method) doesn't have a custom returning type.
Although under the hood, it always return a reference of an object it had just instantiated.
That is, it implicitly returns the special value
this
! >:/8 in total? boolean, byte, character, integer, float, color, String, long, double <- are one of these not a datatype???
whaaaaat? way to throw me off track... you completely lost me here... when you say "can be used as data-type" i guess you mean like button = 5; and then button is int 5? can you mention a few that aren't keywords then? just so i get a better understanding at what you're trying to say (hopefully).
so you wouldn't want to use a void if you're interested in having something returned? or doesn't that matter as you can just add the return;?
hm... then i don't understand the need to use "this" if it doesn't need a return type... you know, unlike void does.
You've mixed 'em all up!!! Let's try it over! :o3
Recapitulating, keywords are internal reserved words of a programming language which can't be used as custom names.
Among those Java keywords, there are 8 which are used to designate primitive data-types:
Besides those 8 listed above,
interface
andclass
names are data-types too!But they're neither primitive nor keywords! They're object data-types instead. Got it!? =:)
Now some extra observations:
Data-type
color
is Processing's own syntactic sugar for Java's primitive data-typeint
!And they can be used interchangeably!!!
Java provides wrapper classes for the 8 primitive data-types:
Boolean, Byte, Short, Character, Integer, Long, Float and Double.
They're object data-types which have the corresponding primitive field variable within them! @-)
And being classes, they all follow capital name convention rule! :P
String is object data-type. It's not primitive! And that, plus Array and Object receive special treatment in Java! (*)
And to prove that String isn't a reserved keyword, look at my snippet below:
I was able to use String as a variable name! And then, as an object data-type for variable txt! $-)
@fiskefyren : you are doing great!
keywords are just reserved words that belong to the language itself such as if, int, for etc.
all variable names or names of functions like whatever or so are not keywords since they don't belong to the language itself.
"this" referes to the sketch you are in or to the class you are in
with this little example you can see the usage of "this" in practice.
The idea is that the variable brand occurs twice: as the parameter of the constructor and as a property in the class. In the constructor, the keyword "this" is used to distinguish between the two: without "this" we refer to the parameter, with "this" we refer to the property of the class since "this" denotes the class itself.
This topic rocks!!!
I would add
sketch - program in processing
Static mode - Static mode simply means it's a list of instructions/calls to existing commands (e.g. draw a bunch of lines then exit). See Active mode
Active mode - Active mode uses the setup() and draw() calls and draw() runs continuously (gets updated every 'frame'). Every advanced sketch is in Active mode. Only here you can have movement, interactivity and usage of functions. See Static mode. From http://stackoverflow.com/questions/6658827/processing-it-looks-like-youre-mixing-active-and-static-modes
Array - is a list. This list can be of different types like int, String or an object. Normally we refer to the 5th entry in array a as a[5]; 5 being the index. The usage of arrays has great advantages: Imagine a game where our hero has 100 enemies. Instead of handling the movement and display of each one in a few lines separately (writing 100s of lines) we can put the enemies in an array and for loop over it and thus repeating a few lines again and again for each enemy. There are also arrays with more dimension than one: An 2 dimensional array is a grid, a 3D array a cube, a 4D array a cube in the 4th dimension (or a series of cubes) and so forth.
2D and 3D - with processing you can draw in 2D space (a canvas like your screen) or in 3D space (a room). In 2D you draw with x and y position, in 3D you paint with x,y and z position where z is the depth. Negative Z leads into the monitor (away from you), positive Z outside the monitor (towards you). Also the camera can be placed in 3D space: it has a position and a point it's looking at (and - seldom used - an angle to allow for fancy effects like leaning left when flying a curve).
@ GoToLoop
I've never heard about "short" before in any of my lectures nor have I ever seen it used... what can it be used for exactly? I don't even know what it's suppose to contain, like how int is full numbers and float is decimal numbers.
Is the data-type color then an advance data-type?
Also I don't understand how your example gives me "A A" as a result, could you explain it so my simple mind can relate? :s
PS. is a type the same thing as a data-type?
@ Chrisir
Hm, I'm trying to understand your example, when I try to run it, it gives me "6 and 0" but I don't understand how +this.brand gives me 0 while brand gives me 6, is it because the car(6) is added to brand but this.brand is only what it says within brand (brand=0)?
Oh, I thought the max of an Array was [][] <-2, btw is there any limitations as to what you can put inside an Array? Like could you put Strings in there or such to recall?
Also I'm having a problem with making an object/array thingy into a global like fx "Movie meow, purr, scratch;" (for sounds) or "Button button1, button2;" because this doesn't work: " objects = new IObjects[2]; " so i'm not sure how to write this in a proper way.
Again, sorry about the late replies! I'm still very interested in this and try to relocate as much time as I can to really understand this! The problem is that I've a lot of other projects on the side line that really complicate things... I dunno, this place is crazy with what they expect of us to do with hardly no time.
I got other questions but I've to look over my material and post again, because I can't remember what I wanted to ask you guys about, but thanks so much so far! It has helped quite a bit <3
In the context I'm using them, they are so! B-)
Data-type
short
is like a miniint
. While anint
takes up 4 bytes (32 bits),short
goes w/ only 2 bytes (16 bits)! :-jAnd like I said, there are 8 primitive data-types in Java. Even though not all schools mention all of them! :-L
Among them,
float
&double
are for fractional values.boolean
fortrue
&false
states.The other 5 primitive data-types are for whole values!
And
color
, as mentioned, is just an alias forint
! <):)I was just trying to prove that only the 8 aforementioned primitive data-types are Java keywords!
The other data-types that come from classes are object data-types and aren't keywords!
As you may know now, we can't use keywords as names for variables, functions and classes.
And even though String is an object data-type, it isn't a keyword like a primitive data-type.
Thus it can be used as a variable name! And that's what I did there!!! @-)
1st, I've declared a
char
variable called String, and printed out its content.2nd, a String variable called txt, and printed that out as well.
Notice how Java doesn't mixed them up! It knows when String is a variable name and when String is an object data-type!!! \m/
Next, the reason why both variables String & txt print out "A":
Data-type
char
, besides storing whole values and taking up 2 bytes (16 bits) just likeshort
, it got further peculiarities:That's why we got an 'A' instead of 65, for that is the ASCII/Unicode value for it! >:/
In the 2nd case, we have ->
String txt = String + "";
, in which String = 65.And again, we got "A" printed out. But this time as an "A" String rather than an 'A'
char
.Object data-type String is nothing more than an array of Unicode characters ->
char[]
!And Java knows that when we convert a
char
into a String, it gotta use its character form rather than its numerical value!!! =:)Well, that's all folks!!! :-\"
my small examples with car brand shows you the meaning of "this": brand does occur twice:
1st as a parameter in the constructor in line 17 (value 6, see line 4). This is like a variable in the constructor only that it is given from the outside as a parameter (line 4).
2nd: as a property of the class (line 9), value 0.
so when we print brand without "this":
we get 6 (parameter)
when we print it with "this", we have 0:
because this refers to the class and therefore the property is referred to.
No, there is no limit really. You can have an array of String or an array of objects even (see next post pls)
Well, you can say
but then you have to say somewhere
or you can say it in one line:
an array of objects is different still;
this code is from another guy.
You can see, in line 1 we init the array with 26 Boxes in it. In line 14 we make an instance (object) box from the class Box. This we do in a double for-loop so the box we invoke is at index k.
We give 5 parameters to the constructor of the box, x,y (position), width and height (size) and the color (strokeColor1). Later we could give it a fill-color (as opposed to stroke-color) and a text (with a textcolor maybe).
Please note that the class Box is written with a capital; one object box would be written with a small letter: box = new Box();
also, in an array we use a small letter (the boxes are objects) but use plural since there are many boxes in the array: boxes.
Hello again! Sorry I haven't replied :/ I've been rather lost in my studies and sadly I failed my programming exam -_- but at least I passed all my other exams (yay). So yeah I'm back again for more help understand this shit... pardon my Danish...
There is 3 days for my re-exam and I talked with my professor and he said I needed to into variables and fundamentals of mouse and drawing, and conditions. Because I apparently failed at those subjects entirely... so if someone could please explain these to me in a way I can understand it (curse me and my dyslexia!), I will be very grateful!
PS. There are also other things I would like to ask. Not to mention that I haven't looked this entire page through, I can see some people have written some things here while I was gone :/ but it's rather late now so I'll look at them tomorrow and write again! But until then I hope that someone will help me with variables and conditions, when I'm done with my lectures tomorrow.
Thanks in advance! You guys have been great so far <3
I'm sorry you failed.
I suggest you go over the first 7 tutorials
http://www.processing.org/tutorials/
what really help is practice so make small sketches with variables and colors and conditions.
use the reference (if e.g.)
http://www.processing.org/reference/
3 days !?
beats me
our last posts (you never said thank you for them) where from november. Since when do you know you failed? November?
Why do you come back that late? You should have come back earlier and practice...
you need to put effort in it when you want to pass...
sorry, I don't meant to sound harsh....
just try some tutorials and some examples...
@Chrisir Sorry, but I'm taking a Bachelor... so I've more topics to cover than just Programming. Beside our grades were delayed for a very long time, so we were just informed not that long ago about who failed and passed...
I haven't been here mostly because of stress and because of busywork. There was also a period where I tried to come back here where the forum was down, I don't know when it came back up running again...
Don't get me wrong, I'm glad for the help... life have just been... bothersome lately...