r/learnjava • • Oct 30 '16

What the HELL is a modulo?

I have a test tomorrow, in the test one of the questions will be to seperate a number from a 3 digit number, say I have 641, I need to know how to print out 6,4 and 1 seperately.

What the hell do I do ? The teacher is so bad I couldn't understand a word she said and neither did my class, we already complained about her but this isn't the issue, the issue is that I have no easy answers on the internet for what the modulo is.

Why does doing (n/100)%10; print out the hundred digit ? I have no idea how this works, please go easy on me.

0 Upvotes

31 comments sorted by

View all comments

0

u/[deleted] Oct 30 '16

[deleted]

1

u/[deleted] Oct 30 '16

I understand it now, the proccess I mean, but the code not, can you please help me out here ?

int n = 741;
	    int t = n%10; // Yahidot
	    System.out.println("Number of Yahidot " + t);
	    int k = (n/10)%10; // Asarot
	    System.out.println("Number of Asarot " + k);
	    int j = n/100; // Meyot
	    System.out.println("Number of Meyot "+ j);

3

u/Sir_Joe Oct 31 '16 edited Oct 31 '16

Alllllright time to try to make an eli5 here.

Disclaimer : For most of you, these explanations may looks like they are tedious, boring, stupid and or something else (or even bad, I'm not gonna pretend that I'm the best guy to explain, I'm just gonna give it a go) . However, I know from experience (one of my closest friend had that problem) that sometime people aren't able to understand a concept that we consider 'basic' simply because they misunderstood (or just don't know) another concept that is required to understand the first one, and that confuse them. I think that's the case of the OP (but I may be wrong). That's why I will try to explain everything as I do not know what he misunderstood or simply didn't understood.


So let's get started :

First some definitions: Integer (int): any number that isn't a fraction; that doesn't include any '.' in it (7.4 isn't a integer).

Variable : You can consider variables like little boxes that contains something of a certain type. For example, you could consider, in the real life, that a shoes box is a variable that contain something of a certain type (shoes in that case but you could store others things in that box right ?). In java the idea is the same but your box (the variable) isn't usually containing shoes; instead they usually contain int (talked about them before) strings (basically text) and a lot of other things that you already know or will in the future. Also technically variables aren't containing the thing itself but instead contain a reference to where the object (or anything else) is in your ram but I guess that's for later.

System.out.println : Scary thing, isn't it :P For now, I would suggest you to simply see this as one of the million of tools Java gives you to program. This tool is pretty simple: You write System.out.println("put something in there") and whatever is in the parenthesis get printed on your console. A useful feature of that tool is that you can print multiples things with only one use. For example lets say I have this line System.out.println("Number of Yahidot " + 5); Java will understand that want you want to say is to print the string Number of Yahidot and the integer 5 in the console.

Modulo : From what I saw from you other comments, I think you understood that part but if you still want different explanation, I will be happy to help.

Comments (anything with // before) : Well there nothing to understand there. In java (and a lot of other programming languages) whenever you put this ' // ' in a line, the computer will ignore anything else past this sign in the same line. They are usually put this in the code in order to give some information to people that read the code on what it actually suppose to do (but you can really write anything you want; Once, one of my friend literally written a freaking poem in comments just for the heck of it in his code.).


Now let give a look of what currently does each lines:

1st : Declare the variable n (create a box named n) that contains the integer 741.

2nd : Declare another variable named t, but this time, instead of simply doing "this variable contains this number" like on the line one, you say this variable contains the result of the following operation : n%10 (741 % 10 in that case). After there is a comment that, I guess, simply mean that the variable t represent the Number of Yahidot.

3rd : This line say to java : print me the sentence "Number of Yahidot " and the value contained in t (in that case it should be 1) in my console.

4: Same thing like the second line but instead of containing the result of the n%10 operation, the variable k will contain the result of this operation: (n/10)%10

5: Well I can continue like that but I think you get the idea. However, don't be afraid to reply back if you feel you would like that I continue.


Finally, If I made any mistake in my explanation (I don't think I did but thats possible) I would like one of you guys tell it to me. I'm also a beginner in java (while perhaps more advanced that OP) and I will be happy to learn from my errors. Also I'm like OP not a native English speaker so please forgive my (sometime) bad English.


Also u/druskim don't give up. I know learning how to program can feel hard at first and you will probably have to accept that you cannot understand everything as long as your able to do it, but believe me, you will be proud later of what you've done and with how the world is headed, I think there is a bright and interesting future for programmers.

Have fun !

1

u/[deleted] Oct 31 '16

I know basically everything you said, just wanted to know why doing a module of 10 and other numbers results in x and y, but thank you so much for the detailed explanation

2

u/Sir_Joe Oct 31 '16 edited Oct 31 '16

I was pretty sure you did. But as I said in my comment, I don't know what you don't know and I wanted to be sure there wasn't anything else you wasn't sure about and do it in one comment. That may be a bit drastic but this way everything is in the same post :P