• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me understand my code  RSS feed

 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there! I've been writing a chat application with the help of a tutorial series from youtube and in the last part we wrote a script that generates a random ID for each client that connects to the server so that we can find his/her credentials using the id.

I just want to make sure i understand how it works,so firstly i will post my script down below,then write what i think this script does and at what order and then hopefully you can help me find out if i was right or wrong?



Ok here i go:

1)We create a list that will hold a bunch of integers
2)We specify an amount of possible ids that we want to generate
3) create an index int so that we can find the position of an element inside the list
4) just the constructor
5) if the i int is less than the Range(=100) then add one to i and then inside the code blocks add that number to the list?(do we use i to put something in that index or just add number 0,1,2,3 for example on some random index of the lsit?)
6) after the for loop is finished, put the numbers that have been assigned into the list in random position for example if  index 0 had num = 2 in it, now it could be 641;
7) checking if we've hit 100 clients connected to the server and if yes we set the index to 0 so that we can generate more ids
8) if the above argument is false, then return a random number(don't really understand this(does ids.get(index++) get the number inside position 0 of the list at first for example and same happens depending on the index's value or does something else happen here?));
9) END?!

Thanks in advance and sorry if this post is not very important for some but i really wanna know that i'm understanding my own code :P !
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the i int is less than the Range(=100) then add one to i and then inside the code blocks add that number to the list?(do we use i to put something in that index or just add number 0,1,2,3 for example on some random index of the lsit?) 

Almost.  You start with i at zero and loop while i is less than the range -- so 0 through 999.  The number that i holds in added to the list.
don't really understand this(does ids.get(index++) 

Let's start inside the parentheses.  You have index with a value of zero initially.  Use that value, then increment the value by one.  So after the first call, index will hold 1.

So initially index is 0, so we have the equivalent of this: ids.get(0).  This gets the first ID in the list.  After the first call, it will be ids.get(1), so it will get the second ID in the list, and so on.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
if the i int is less than the Range(=100) then add one to i and then inside the code blocks add that number to the list?(do we use i to put something in that index or just add number 0,1,2,3 for example on some random index of the lsit?) 

Almost.  You start with i at zero and loop while i is less than the range -- so 0 through 999.  The number that i holds in added to the list.
don't really understand this(does ids.get(index++) 

Let's start inside the parentheses.  You have index with a value of zero initially.  Use that value, then increment the value by one.  So after the first call, index will hold 1.

So initially index is 0, so we have the equivalent of this: ids.get(0).  This gets the first ID in the list.  After the first call, it will be ids.get(1), so it will get the second ID in the list, and so on.


Awesome, thanks a lot for this m8 ! Everything else that i mentioned was correct i guess except from these 2 things?
[PS. So, in the for loop we just add that number in the list but in which order? Do we do it like when i = 0 we put it in ids.add in position 0 and so on or not this way?

Thanks again!
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stelios papamichael wrote:5) if the i int is less than the Range(=100) then add one to i and then inside the code blocks add that number to the list?(do we use i to put something in that index or just add number 0,1,2,3 for example on some random index of the lsit?)
Not really. First, your RANGE is 1000, not 100. Second, you don't increment i right away, but rather you add current i value to list and only then increment i and then again you check if i is less than RANGE (=1000).

stelios papamichael wrote:7) checking if we've hit 100 clients connected to the server and if yes we set the index to 0 so that we can generate more ids
Again, not sure where you take this 100 and what it has to do with server clients. This static method getIdentifier() checking if current index have passed the last element position within the list, in case it did, it resets index to 0 and again returns first element in the list, then second and so on. Your comment says that it returns random number inside the list. Your so called random number maybe comes from the fact that list has been shuffled, but that is it, the index itself isn't random, so you know which element within the list you're going to get.

And how the heck you managed to write the code which you don't understand Could you explain a bit more about your project? Did you have any requirements you tried to follow? (want to post them here?) Was this program only for practice purpose taken from the thin air?

After all: Have you tried to compile this code? Have you tried to run it? What output you observed? Did the output match your expected output?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, in the for loop we just add that number in the list but in which order? Do we do it like when i = 0 we put it in ids.add in position 0 and so on or not this way? 

In the for loop, before Collections.shuffle(), the ids list has all its numbers "in order", that is, ids.get(0) would return 0, ids.get(1) would return 1, etc.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
stelios papamichael wrote:5) if the i int is less than the Range(=100) then add one to i and then inside the code blocks add that number to the list?(do we use i to put something in that index or just add number 0,1,2,3 for example on some random index of the lsit?)
Not really. First, your RANGE is 1000, not 100. Second, you don't increment i right away, but rather you add current i value to list and only then increment i and then again you check if i is less than RANGE (=1000).

stelios papamichael wrote:7) checking if we've hit 100 clients connected to the server and if yes we set the index to 0 so that we can generate more ids
Again, not sure where you take this 100 and what it has to do with server clients. This static method getIdentifier() checking if current index have passed the last element position within the list, in case it did, it resets index to 0 and again returns first element in the list, then second and so on. Your comment says that it returns random number inside the list. Your so called random number maybe comes from the fact that list has been shuffled, but that is it, the index itself isn't random, so you know which element within the list you're going to get.

And how the heck you managed to write the code which you don't understand Could you explain a bit more about your project? Did you have any requirements you tried to follow? (want to post them here?) Was this program only for practice purpose taken from the thin air?

After all: Have you tried to compile this code? Have you tried to run it? What output you observed? Did the output match your expected output?


Like i mentioned this is a project that i am doing while following a tutorial on youtube.The code ran perfectly and everything worked, i just wanted to be sure that i do understand every single line!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!