hey everybody i need an answer of this question plz as soon as possible ,
write a method named season that takes two integers as parameters representing a month and day and that returns a string indicating the season for that month and day. Assume that months are specified as an integer between 1 an 12 (1 for jun, 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 return "winter" , if the date falls 3/16 and 6/15 you should return "spring" , if the date falls 6/16 and 9/15 you should return "summer" and if the date falls between 9/16 and 12/15 you should return "fall"??

3
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by HibaPro

If you are looking for someone to give you codes, I would discourage you because I will not give you the code and would expect others to do the same.

If you are looking for how to implement this, I could at least help you out.

Look at the season month first. You can create a range for the season months.

Spring  3~6
Summer  6~9
Fall    9~12
Winter  12 and 1~3

You need to check for the month first. If the input is valid (day & months are in the valid range), there will be only 2 cases to check for the month.

Case 1: The month value falls in the middle of a season range, you found the season (i.e. 2 is in between 1~3 which is winter), return the season.
Case 2: The month value falls in the edge of season range, you need to check for the day range also. i.e. 6 is either in Spring or Summer. Check the day value. If the day value is 15 or lower, it is Spring; otherwise, it is Summer.

That's all.

I did a quick implementation in JavaScript but took a different approach. I look at the day first to see if it's greater than 15 or not. I created a five-element array of the seasons with 'winter' appearing both as the first and last elements of the array. Then I just used math to automatically calculate the corresponding array index and return the appropriate season. I have a total of three conditional statments (if, else if, else).

Edited by dcdruck: Clarification

There are many ways to implement a solution. The solution I gave would require no extra space but if/else because everything is constant. The reason I check for month first because it will reduce the path of checking and required less memory usage.

I do not say that your solution is wrong, but it is just another alternative solution. :)

Edited by Taywin

i dont know a java to write a code but i need this code to implement on other project after that what i can do ??

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.