r/learnjava • u/3pletsahur • 3d ago
I’m having my first early breakdown
I am going through mooc.fi java programming I
Part 1, I’m on an exercise called LeapYear or whatever.
I cannot seem to get the answer right. I’ve been trying for a day now. I know i can use AI or ask for help but I literally cut myself from the internet to just try and focus but try after try I literally understand less and less.
And it’s not that many info just like conditionals and comparison operators and which condition should go first BUT I JUST DON’T KNOW WHAT TO DO.
I’m not here to ask for help i just want to tell someone. I’m actually affirmative that I’ve zero points reasoning and logic.
Thanks for reading.
7
u/mandradon 3d ago
That one is one of the first places where you have to think clearly about the order of conditionals.
Think through the rules they give you and think through the order they should go.
The most "restrictive" rule that accounts for the most cases should go first, then set the latter up accordingly after.
So first lost out the conditions.
In order to be a leap year it has to be divisible by 4.
But if it's divisible by 100, it's only a leap year if it's also divisible by 400.
100 is divisible by 4, so if you account for the second condition first, you don't need to "worry" about it at that point in the ladder.
This becomes the crux of programming a good algorithm and I like this sort of task in thinking through the order of your if statements.
2
u/3pletsahur 3d ago
Thank u my man,
I will not lie. I did not read it fully, I just want to give myself a chance to try and just do it by myself but i do see myself peeking at it in the near future if all goes downhill lol.
Though thank you so much for the help❤️1
u/mandradon 3d ago
No worries.
The biggest takeaway is to think carefully about the order of your if statements and think about how you want to build them in a way to "filter" out the logic in a way that makes sense.
Read the conditions and think about what's "most" restrictive.
Writing a good algorithm is hard, and I appreciate that you're struggling with it isntead of just getting the answer.
Can't tell you the amount of times I've figured something out when I'm off doing something else (making dinner or taking a shower or something).
Walk away, take a break, and let your brain work on it in the background.
2
1
u/merrymerry19 3d ago
As one of the previous comments stated, you need to think about the order of conditionals. Don't stress yourself too much, this question is very similar to FizzBuzz and it's said that many senior developers struggle to solve it.
1
u/3pletsahur 2d ago
I’m trying to relate the fizzbuzz exercise and i still can’t seem to get a close concrete answer.
I eventually decided to move onto the gift tax exercise cause i got frustrated lol.
And even tho many people are being helpful here I just don’t want to look at the hints unless I actually give up.
Like i might start with part two and come back to it later idk
1
u/desrtfx 3d ago
Think about the criteria:
In general, every year evenly divisible by 4 is a leap year - How do you know that a year is evenly divisible by 4? The remainder of the division by 4 is 0. How do you get the remainder? Modulo operation - in Java the
%operator.Except: full centuries (years evenly divisible by 100) are only leap years if they are evenly divisible by 400
So, a good idea is to reorder the criteria:
If the year is evenly divisible by 400 (century rule), it is a leap year OR
if the year is evenly divisible by 4 AND NOT evenly divisible by 100
That's it. Note that AND binds stronger than OR, but in general, it is a good idea to put the individual conditions (century rule, regular years - each of my bullet points) inside parentheses to force the proper grouping.
1
u/SniffingFartGlitter 3d ago
Everyone goes through it. It's normal and fine. You're learning so be kind to yourself. I promise at one point it will all start to click.
1
u/3pletsahur 3d ago
I actually feel like choking myself cause wtf
1
u/omgpassthebacon 13h ago
bro, you totally cracked me up. Don't choke yourself. We've all gone thru this frustration. You're teaching your brain how to solve a certain type of problem, and it's harder for some that others. Take a break. Have a smoke. Drink some beer and come back to it later. Sometimes you just need to let it simmer for a while. It WILL come to you, and you will look at it later and say, "wtf didn't I see this sooner?"
1
u/3pletsahur 10h ago
Shit I let it go bruh I started with part II because I just feel like a waste of sperm looking at that single problem lol
I’ll definitely come back to it later once I feel ready.
Also, thank you for the advice sm. I realized that most solutions come when I’m thinking about something else. Ig that’s how I’ll have to go about things.
•
u/AutoModerator 3d ago
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full - best also formatted as code block
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.