SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Conditional Execution
Exercise:
Exercises
Question:3 | ISBN:9780136091813 | Edition: 2

Question

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".

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.