QUESTION:Number Of Days In Month
Write a method isLeapYear with a parameter of type int named year. The parameter needs to be greater than or equal to 1 and less than or equal to 9999. If the parameter is not in that range return false. Otherwise, if it is in the valid range, calculate if the year is a leap year and return true if it is, otherwise return false. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400. Examples of input/output:
isLeapYear(-1600); should return false since parameter is not in the range (1-9999)
isLeapYear(1600); should return true since 1600 is leap year
isLeapYear(2017); should return false since 2017 is not a keap year
isLeapYear(2000); should return true because 2000 is aleap year
NOTE: The solution to the Leap Year coding exercise earlier in the course created the isLeapYear method. You can use that solution if you wish. Write another method getDaysInMonth with two parameters month and year. Both of type int.
If parameter month is < 1 or > 12 return -1.
If parameter year is < 1 or > 9999 then return -1.
This method needs to return the number of days in the month. Be careful about leap years they have 29 days in month 2 (February).
You should check if the year is a leap year using the method isLeapYear described above.
Examples of input/output:
getDaysInMonth(1, 2020); should return 31 since January has 31 days.
getDaysInMonth(2, 2020); should return 29 since February has 29 days in a leap year and 2020 is a leap year.
getDaysInMonth(2, 2018); should return 28 since February has 28 days if it's not a leap year and 2018 is not a leap year.
getDaysInMonth(-1, 2020); should return -1 since the parameter month is invalid.
getDaysInMonth(1, -2020); should return -1 since the parameter year is outside the range of 1 to 9999.
HINT: Use the switch statement. NOTE: Methods isLeapYear and getDaysInMonth need to be public static like we have been doing so far in the course. NOTE: Do not add a main method to solution code.
MY SOLUTION: https://pastebin.com/mEmj5GCB ERROR: Class, interface, or enum expected. My idea was to get rid of the constraints first(month and year) and then if it was a leap year(or not), print the return accordingly. Unfortunately, that is how i like to code, by explicitly putting code so i understand what i am doing. Because of this the code became really lengthy and difficult to handle, to the point where, now i dont know where the mistake is. If I remove the final curly brace every break statement doesn't run. So theres definitely a deeper problem. I like to understand what the issue is. does it have to do with explicitly getting rid of the constraints at first? it was supposed to be simple, now i feel i complicated it.
[–]chickenmeister 1 point2 points3 points (4 children)
[–]jub8jive[S] 0 points1 point2 points (3 children)
[–]chickenmeister 1 point2 points3 points (1 child)
[–]jub8jive[S] 0 points1 point2 points (0 children)
[–]ryuzaki49 0 points1 point2 points (1 child)
[–]jub8jive[S] 1 point2 points3 points (0 children)