Write a method called season that takes as parameters two integers representing a month and day and returns a String indicating the season for that month and day. Assume that the month is 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, the method should return "winter". If the date falls between 3/16 and 6/15, the method should return "spring". If the date falls between 6/16 and 9/15, the method should return "summer". And if the date falls between 9/16 and 12/15, the method should return "fall".
Program:
public class Ch04Ex03
{
public static void main(String[] args)
{
System.out.println("season(12, 10): " + season(12, 10));
System.out.println("season(12, 20): " + season(12, 20));
System.out.println("season(4, 10): " + season(4, 10));
System.out.println("season(8, 20): " + season(8, 20));
}
public static String season(int month, int day)
{
if((month == 12 && day >= 16) || month == 1 || month == 2 || (month == 3 && day <= 15))
{
return "winter";
}
else if((month == 3 && day >= 16) || month == 4 || month == 5 || (month == 6 && day <= 15))
{
return "spring";
}
else if((month == 6 && day >= 16) || month == 7 || month == 8 || (month == 9 && day <= 15))
{
return "summer";
}
else if((month == 9 && day >= 16) || month == 10 || month == 11 || (month == 12 && day <= 15))
{
return "fall";
}
else
{
return "Invalid season!";
}
}
}
Output:
season(12, 10): fall
season(12, 20): winter
season(4, 10): spring
season(8, 20): summer