# Seasons by month and day

Write a program reads two integers representing a month and day and prints the season for that month and day. Assume that months are specified as an integer between 1 and 12 (1 for January, 2 for February, and so on) and that the day of the month is a number between 1 and 31. If the date falls between 12/16 and 3/15, you should print "Winter". If the date falls between 3/16 and 6/15, you should print "Spring". If the date falls between 6/16 and 9/15, you should print "Summer". And if the date falls between 9/16 and 12/15, you should print "Fall".

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

int month = scan.nextInt();
int day = scan.nextInt();

if((month == 12 && day >= 16 && day <= 31) || (month == 1 && day >=1 && day <= 31) || (month == 2 && day >= 1 && day <= 28) || (month == 3 && day >=1 && day <=15))
{
System.out.println("Winter");
}
else if((month == 3 && day >= 16 && day <= 31) || (month == 4 && day >=1 && day <= 30) || (month == 5 && day >= 1 && day <= 31) || (month == 6 && day >=1 && day <=15))
{
System.out.println("Spring");
}
else if((month == 6 && day >= 16 && day <= 30) || (month == 7 && day >=1 && day <= 31) || (month == 8 && day >= 1 && day <= 31) || (month == 9 && day >=1 && day <=15))
{
System.out.println("Summer");
}
else if((month == 9 && day >= 16 && day <= 30) || (month == 10 && day >=1 && day <= 31) || (month == 11 && day >= 1 && day <= 30) || (month == 12 && day >=1 && day <=15))
{
System.out.println("Fall");
}
}


}

• And you either are missing the starting line public class Something or you have one too many closing } – Caridorc Oct 20 '15 at 19:30
• Nah that's not it :/ – Kj45 Oct 20 '15 at 19:33
• I mean in the post, not in your code in your computer – Caridorc Oct 20 '15 at 19:35
• The thing is I've sent the code in my computer to an online judge and it gave me a score of 98/100! So, there has to be something very small that is so hard to find. – Kj45 Oct 20 '15 at 19:37
• We do not modify code behaviour... I mean we are happy about making the code nicer and faster, but fixing a corner case is not really on-topic... Unless the judge also counts execution time – Caridorc Oct 20 '15 at 19:39

Your conditional checks are very very long. They run off the screen. At the very least, add some new lines:

else if ((month == 9 && day >= 16 && day <= 30) ||
(month == 10 && day >=1 && day <= 31) ||
(month == 11 && day >= 1 && day <= 30) ||
(month == 12 && day >=1 && day <=15))


But this still makes it very hard to actually understand what is going on. There's no really any notion of "range" in this code. You are simply hardcoding each individual month separately.

Ranges

Representing the range "3/16 to 6/15" is difficult. Representing the range 316 to 615 is easy. If we don't care about checking for invalid dates, we can uniquely represent the year by doing month * 100 + day and looking at it that way.

int month_day = month * 100 + day;
if (month_day <= 315) {
System.out.println("Winter");
}
else if (month_day <= 615) {
System.out.println("Spring");
}
else if (month_day <= 915) {
System.out.println("Summer");
}
else if (month_day <= 1215) {
System.out.println("Fall");
}
else {
System.out.println("Winter");
}


By treating the ranges as one-sided, it's easy to organize the code to actually look like we're looking for ranges. Winter simply appears on both ends for simplicity.

• Nitpick: I wouldn't use underscores (so, monthDay instead of month_day - underscore convention is alien to Java). Clever trick anyhow! – Konrad Morawski Oct 20 '15 at 20:28

My suggestion - encapsulated in a method to allow for calling itself:

void printSeason(int month, int day)
{
switch (month)
{
case 1:
case 2:
System.out.println("Winter");
break;

case 4:
case 5:
System.out.println("Spring");
break;

case 7:
case 8:
System.out.println("Summer");
break;

case 10:
case 11:
System.out.println("Fall");
break;

// "ambiguous" months
case 3:
case 6:
case 9:
case 12:
boolean firstHalf = day <= 15;
// during first half of the month it's same season as previous month.
// second half of the month = same season as next month.
int adjacentMonth = month + (firstHalf ? 1 : -1);