|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
// recursive computation of sum of ages of all players in the list int sumAge(PlayerList list) { if(firstPlayer.getNext() == null) { return firstPlayer.getAge(); } else { return head().getAge() + sumAge(tail()); } }
plus
PlayerList tail() { PlayerList list = new PlayerList(head().getNext()); if(list.head() == null) { final PlayerList empty = new PlayerList(); return empty; } return list; }
is absolutely gnarly logic. sumAge() always relies on the Player's getNext() to be null as a base case. However, in tail(), you check the same scenario, and return a new PlayerList with a null firstPlayer inside of it. So when you do the recursive call on sumAge(), the condition is now checking if a null object's getNext() is null...I don't really understand why this is even possible, lol. You really should be hitting NullReferenceExceptions, but maybe the StackOverflowError is resulting from this as a compensatory side effect somewhere?
It's dangerous to assume that objects being passed in are always going to be valid. You might prevent a lot of headaches if you check that firstPlayer itself is null instead.
On a side note, in tail(), why do you create another new list if the head is null? You're basically returning the exact same object.
|
Oh and for future reference, one quick look at the call stack should answer your future stack overflow questions rather quickly .
|
Okay i just took a look at the stack trace ( i think ?^^ ) it's like this :
+ Show Spoiler + Exception in thread "main" java.lang.StackOverflowError at PlayerList.<init>(PlayerList.java:12) at PlayerList.tail(PlayerList.java:36) at PlayerList.sumAge(PlayerList.java:115) at PlayerList.sumAge(PlayerList.java:115) at PlayerList.sumAge(PlayerList.java:115) at PlayerList.sumAge(PlayerList.java:115)
Could the problem be that when i call the constructor with PlayerList( head().getNext()) and it is null that it causes problems? I'm bit confused i have to try again later when i m back at home..
|
Hi guys, quick semi-programming question.
I've been set a task to document a section of our codebase, a server which displays IP Camera feeds and its interactions with a specific DLL which drives the station which provides the feeds.
I never did CS at university so I'm a little unsure about how to approach this, I submitted a first draft and got some useful feedback but for the final document I'd like to try and provide a more visual explanation, are there any tools for doing this? I know that Visual Studio Ultimate has tools for this but I'm only on professional, bearing in mind this is all written in C for Windows are there are good visualisation tools or should I just crack open paint / something to just make some diagrams?
Any advice or examples would be hugely appreciated thanks!
|
@adwidon, I'm not sure if I really picked up on what you are looking for, but this might help: http://diagramo.com/ (or maybe just open-office draw!)
Hope it helps
|
On December 19 2013 16:48 Vilanoil wrote:Okay i just took a look at the stack trace ( i think ?^^ ) it's like this : + Show Spoiler + Exception in thread "main" java.lang.StackOverflowError at PlayerList.<init>(PlayerList.java:12) at PlayerList.tail(PlayerList.java:36) at PlayerList.sumAge(PlayerList.java:115) at PlayerList.sumAge(PlayerList.java:115) at PlayerList.sumAge(PlayerList.java:115) at PlayerList.sumAge(PlayerList.java:115)
Could the problem be that when i call the constructor with PlayerList( head().getNext()) and it is null that it causes problems? I'm bit confused i have to try again later when i m back at home..
Well, since this is pretty obviously (I think?) a homework assignment I'll stay in the spirit of not giving you the answer straight up, but here's a hint... think about which of your two sumAge functions gets called when you call sumAge(tail()). Especially important, make sure you're thinking about the implicit this - both in the definition and in the call ^^
also, I'm not a good Java programmer by any means so this might be more obvious if you've used the language a bit but why in all glorious hell have you defined two versions of all your methods?
|
I know it's a slight detail, but I'd like to read some opinions. Let's say you need to count lines but you need to do something specific only for the 1st line, is it better to:
boolean firstLine = true;
while (some condition) { if (firstLine) { // do something firstLine = false; } else { // deal with next lines } }
or is it better to:
while (some condition) { if (numberOfLines == 1) { // do something } else { // deal with next lines } }
As I said, I know it's a minor detail but I'd rather do it properly. I know people say "don't use magic numbers", hence my uncertainty here.
|
When talking about minor details I cant answer your question but I can add that I would rather see you name "numberOfLines" to something more describing such as "lineNumber". I know you might not have english as your first language but numberOfLines would to me be seen as the totalNumberOfLines. Maybe that is what you want I dunno it is hard to answer questions in a vacuum.
To your question again, if you are using your variable "numberOfLines" for something else as well I think the second option is best as you do not need to add another variable to keep track of.
|
Why even do that in the while loop? Just do it before and then start the while loop at the second line
|
On December 20 2013 00:46 njt7 wrote: When talking about minor details I cant answer your question but I can add that I would rather see you name "numberOfLines" to something more describing such as "lineNumber". I know you might not have english as your first language but numberOfLines would to me be seen as the totalNumberOfLines. Maybe that is what you want I dunno it is hard to answer questions in a vacuum.
To your question again, if you are using your variable "numberOfLines" for something else as well I think the second option is best as you do not need to add another variable to keep track of.
Thanks, you have a point about naming.
On December 20 2013 00:52 Yoshi- wrote: Why even do that in the while loop? Just do it before and then start the while loop at the second line
Actually, yes. It would make more sense if I do it your way. Thanks.
|
On December 19 2013 18:03 Cyx. wrote:Show nested quote +On December 19 2013 16:48 Vilanoil wrote:Okay i just took a look at the stack trace ( i think ?^^ ) it's like this : + Show Spoiler + Exception in thread "main" java.lang.StackOverflowError at PlayerList.<init>(PlayerList.java:12) at PlayerList.tail(PlayerList.java:36) at PlayerList.sumAge(PlayerList.java:115) at PlayerList.sumAge(PlayerList.java:115) at PlayerList.sumAge(PlayerList.java:115) at PlayerList.sumAge(PlayerList.java:115)
Could the problem be that when i call the constructor with PlayerList( head().getNext()) and it is null that it causes problems? I'm bit confused i have to try again later when i m back at home.. Well, since this is pretty obviously (I think?) a homework assignment I'll stay in the spirit of not giving you the answer straight up, but here's a hint... think about which of your two sumAge functions gets called when you call sumAge(tail()). Especially important, make sure you're thinking about the implicit this - both in the definition and in the call ^^ also, I'm not a good Java programmer by any means so this might be more obvious if you've used the language a bit but why in all glorious hell have you defined two versions of all your methods?
Yes your right, i had to finish that exercise for my lab, but i stated that in the my first post I really have to thank guys for your reply's, i just started it again from scratch and it works now. I realized that i made some mistakes at the call of tail(). I always called it with out an object ! so basically the method tried to create a tail and had nothing to work with.
why in all glorious hell have you defined two versions of all your methods? For example my project:
int sumAge(PlayerList list){ //stuff }
int sumAge(){ return sumAge(this); } // is called in main with players.sumAge();
I'm not sure but it seems like that is a recursive call of sumAge() and that (this) is automatically referring to the list players. Would be awesome if someone could explain this.
|
So I've been learning python (My main book is "How to think like a Computer Scientist"). Eventually though I'd like to create a financial management app on Django. Can anyone recommend me a book or an online course that focuses on the web programming of things? Something that deals with databases, API integrations, Django stuff in particular?
|
On December 20 2013 08:48 lannisport wrote: So I've been learning python (My main book is "How to think like a Computer Scientist"). Eventually though I'd like to create a financial management app on Django. Can anyone recommend me a book or an online course that focuses on the web programming of things? Something that deals with databases, API integrations, Django stuff in particular?
Django is quite popular, you should be able to find beginner tutorials easily on the net. After that gets you started this book is very highly regarded, and is up to date too:
http://www.amazon.com/Two-Scoops-Django-Best-Practices/dp/1481879707/ref=sr_1_1?s=books&ie=UTF8&qid=1387497972&sr=1-1&keywords=django
Happy hacking
|
On December 20 2013 07:49 Vilanoil wrote: int sumAge(PlayerList list){ //stuff }
int sumAge(){ return sumAge(this); } // is called in main with players.sumAge();
I'm not sure but it seems like that is a recursive call of sumAge() and that (this) is automatically referring to the list players. Would be awesome if someone could explain this.
All this is really doing is giving you a shorthand to sum the ages of the PlayerList this method is being called from. Ex.
PlayerList a; PlayerList b; // pretend these have stuff in it
a.sumAge(b); // performs the operation on PlayerList b (I assume summing up the ages...) a.sumAge(); // calls the non-parameterized one, then falls back to the parameterized method
// Basically...
a.sumAge() == a.sumAge(a);
Let me know if that doesn't make sense! It seems like a strange implementation, but that is the literal way that it is working.
|
On December 20 2013 09:26 Maero wrote:Show nested quote +On December 20 2013 07:49 Vilanoil wrote: int sumAge(PlayerList list){ //stuff }
int sumAge(){ return sumAge(this); } // is called in main with players.sumAge();
I'm not sure but it seems like that is a recursive call of sumAge() and that (this) is automatically referring to the list players. Would be awesome if someone could explain this. All this is really doing is giving you a shorthand to sum the ages of the PlayerList this method is being called from. Ex. PlayerList a; PlayerList b; // pretend these have stuff in it
a.sumAge(b); // performs the operation on PlayerList b (I assume summing up the ages...) a.sumAge(); // calls the non-parameterized one, then falls back to the parameterized method
// Basically...
a.sumAge() == a.sumAge(a);
Let me know if that doesn't make sense! It seems like a strange implementation, but that is the literal way that it is working.
But I guess the question is... good lord, why, please why something so awfully confusing? players.sumAge() will call sumAge(this)... calling players.sumAge(this) within that function just means you end up calling sumAge(this, this) basically. It's literally just adding an extra definition and extra confusion to something that should be one method. I see absolutely zero sense in doing it this way instead of just writing sumAge() to do the actual summing of ages, and then not bother with the definition that has an extra parameter (which never even gets used).
|
On December 20 2013 00:24 darkness wrote:I know it's a slight detail, but I'd like to read some opinions. Let's say you need to count lines but you need to do something specific only for the 1st line, is it better to: boolean firstLine = true;
while (some condition) { if (firstLine) { // do something firstLine = false; } else { // deal with next lines } }
or is it better to: while (some condition) { if (numberOfLines == 1) { // do something } else { // deal with next lines } }
As I said, I know it's a minor detail but I'd rather do it properly.  I know people say "don't use magic numbers", hence my uncertainty here. As some pointed out, it is better to move the code outside of the loop if possible. But sometimes it is more trouble than it is worth and then your question is still valid. In those cases I would say if you already have some iteration count, just use it, otherwise use the bool as it is much clearer what the purpose of the condition is.
Possible scenario where moving code outside of the loop is maybe not the best idea :
boolean firstLine = true;
while (some condition) { if (firstLine) { // do something firstLine = false; } // do something else for all lines including first }
This often pops up when manipulating string and similar stuff. In this case moving the code for first line out of the loop necessitates duplication of code and in general I would say you should not do it.
|
On December 20 2013 09:10 Shikada wrote:Show nested quote +On December 20 2013 08:48 lannisport wrote: So I've been learning python (My main book is "How to think like a Computer Scientist"). Eventually though I'd like to create a financial management app on Django. Can anyone recommend me a book or an online course that focuses on the web programming of things? Something that deals with databases, API integrations, Django stuff in particular? Django is quite popular, you should be able to find beginner tutorials easily on the net. After that gets you started this book is very highly regarded, and is up to date too: http://www.amazon.com/Two-Scoops-Django-Best-Practices/dp/1481879707/ref=sr_1_1?s=books&ie=UTF8&qid=1387497972&sr=1-1&keywords=djangoHappy hacking 
Wow that's exactly what I was looking for thanks! There's even a chapter specifically on REST APIs.
|
On December 20 2013 10:11 Cyx. wrote:Show nested quote +On December 20 2013 09:26 Maero wrote:On December 20 2013 07:49 Vilanoil wrote: int sumAge(PlayerList list){ //stuff }
int sumAge(){ return sumAge(this); } // is called in main with players.sumAge();
I'm not sure but it seems like that is a recursive call of sumAge() and that (this) is automatically referring to the list players. Would be awesome if someone could explain this. All this is really doing is giving you a shorthand to sum the ages of the PlayerList this method is being called from. Ex. PlayerList a; PlayerList b; // pretend these have stuff in it
a.sumAge(b); // performs the operation on PlayerList b (I assume summing up the ages...) a.sumAge(); // calls the non-parameterized one, then falls back to the parameterized method
// Basically...
a.sumAge() == a.sumAge(a);
Let me know if that doesn't make sense! It seems like a strange implementation, but that is the literal way that it is working. But I guess the question is... good lord, why, please why something so awfully confusing? players.sumAge() will call sumAge(this)... calling players.sumAge(this) within that function just means you end up calling sumAge(this, this) basically. It's literally just adding an extra definition and extra confusion to something that should be one method. I see absolutely zero sense in doing it this way instead of just writing sumAge() to do the actual summing of ages, and then not bother with the definition that has an extra parameter (which never even gets used).
We completely agree! But he asked for an explanation of how it was working, so that's what I provided  The note at the end about it being a strange implementation was alluding to your point - there's no real good reason to put it together in that way and either one or the other would work fine (depending on how the method interacts with other PlayerList age sums).
|
On December 20 2013 11:02 Maero wrote:Show nested quote +On December 20 2013 10:11 Cyx. wrote:On December 20 2013 09:26 Maero wrote:On December 20 2013 07:49 Vilanoil wrote: int sumAge(PlayerList list){ //stuff }
int sumAge(){ return sumAge(this); } // is called in main with players.sumAge();
I'm not sure but it seems like that is a recursive call of sumAge() and that (this) is automatically referring to the list players. Would be awesome if someone could explain this. All this is really doing is giving you a shorthand to sum the ages of the PlayerList this method is being called from. Ex. PlayerList a; PlayerList b; // pretend these have stuff in it
a.sumAge(b); // performs the operation on PlayerList b (I assume summing up the ages...) a.sumAge(); // calls the non-parameterized one, then falls back to the parameterized method
// Basically...
a.sumAge() == a.sumAge(a);
Let me know if that doesn't make sense! It seems like a strange implementation, but that is the literal way that it is working. But I guess the question is... good lord, why, please why something so awfully confusing? players.sumAge() will call sumAge(this)... calling players.sumAge(this) within that function just means you end up calling sumAge(this, this) basically. It's literally just adding an extra definition and extra confusion to something that should be one method. I see absolutely zero sense in doing it this way instead of just writing sumAge() to do the actual summing of ages, and then not bother with the definition that has an extra parameter (which never even gets used). We completely agree! But he asked for an explanation of how it was working, so that's what I provided  The note at the end about it being a strange implementation was alluding to your point - there's no real good reason to put it together in that way and either one or the other would work fine (depending on how the method interacts with other PlayerList age sums).
okie, cool =) I guess I just wasn't sure if it was like... a Java thing or something (I mostly use C++) or if it was as totally weird as it seemed. @Vilanoil: were you given the code like that (for your class) or did you write the whole PlayerList class yourself?
|
Finished my term at Xtreme/Pivotal Labs! Great programming company in Toronto, visit them if you want a really good internship or a good full-time opportunity.
|
|
|
|
|
|