Week 5, exercise 92 - Technical issue (?)

286 views
Skip to first unread message

asma...@gmail.com

unread,
Nov 13, 2015, 1:10:18 PM11/13/15
to mooc.fi
Hello!

I seem to be having some trouble with my submission. I have written code that works on my PC:

https://tmc.mooc.fi/mooc/paste/xBT1V6krwX4za_8ETzO1Nw

but for some reason I keep getting the following error:

"Add class MyDate method public int differenceInYears(MyDate compared)"

Is there something wrong with my code? If so, how could I fix it?
Thank you in advance!

Best regards,
John

P.S.: In my PasteBin upload I have made some comments, but in the code that I submitted the comments were not there.

asma...@gmail.com

unread,
Nov 13, 2015, 1:37:06 PM11/13/15
to mooc.fi, asma...@gmail.com
I also seem to be getting a "Stackoverflow" error in exercise 93. Could it be linked to this? I used the same method (differenceInYears) in that exercise. Any help would be much appreciated!

ljleppan

unread,
Nov 14, 2015, 6:42:59 AM11/14/15
to mooc.fi, asma...@gmail.com
Hi John,

The Stack Overflow essentially means that you have a method somewhere that is calling itself forever and forever. So essentially a pattern like

public void someMethod() {
  someMethod
()
}


For the first method call to finish, it needs to wait for the inner method call to finish. For the inner method call to finish, it needs to wait for the inner-inner method call to finish and so on ad infinum. So no method calls finish and you eventually run out of memory.

In this case, the issue seems to be caused by the differenceInYears() method: Consider a case where the two dates under comparison have the same year. You never enter the if-block, but always just keep on calling the other date's differenceInYears() method. This keep going until Java runs out of memory and you get the StackOverflowError.

It's likely that this issue also prevents you from passing the final test. If the code still won't pass the tests after fixing the StackOverflowError, please create a new paste and I'll take a further look :)

-Leo

asma...@gmail.com

unread,
Nov 14, 2015, 8:54:51 AM11/14/15
to mooc.fi, asma...@gmail.com
Hey Leo!

As usual, your remark was just what I needed to fix my code! I resolved the issue of equal dates by changing to "else if" rather than "else", and then returning 0 after the if clause was complete, namely only if the dates were actually equal. All tests pass for both exercises now.

Thank you so much!

Best regards,
John
Reply all
Reply to author
Forward
0 new messages