forked from jusju/lambda-harjoitus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDayOfYear.java
More file actions
70 lines (66 loc) · 2.55 KB
/
DayOfYear.java
File metadata and controls
70 lines (66 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package refactoring;
/**
* Smelly Example #1
*
* https://web.mit.edu/6.005/www/fa16/classes/04-code-review/
*
* Collaboratively authored with contributions from: Saman Amarasinghe, Adam
* Chlipala, Srini Devadas, Michael Ernst, Max Goldman, John Guttag, Daniel
* Jackson, Rob Miller, Martin Rinard, and Armando Solar-Lezama.
*
* This work is licensed under CC BY-SA 4.0.
*/
public class DayOfYear {
/**
* "The day of year (DOY) is the sequential day number starting with day 1 on
* January 1st."
*
* https://nsidc.org/data/user-resources/help-center/day-year-doy-calendar
*
* This method calculates the day of the year (1-366) that corresponds to the
* given month, day of month, and year. January 1st is day 1 of the year and
* December 31st is day 365 (or 366 in a leap year).
*
* @param month the month (1-12)
* @param dayOfMonth the day of the month (1-31)
* @param year the year
* @return the day of the year (1-366)
*/
public static int dayOfYear(int month, int dayOfMonth, int year) {
/*
* You are expected to change this method in the exercise in order to fix the
* bugs in it. You are also expected to improve the method's structure and
* style to make it more readable and maintainable.
*
* You are allowed to throw runtime exceptions, such as
* IllegalArgumentException, if the inputs are invalid.
*
* However, you are *not* allowed to change the method signature (name,
* parameters, return type).
*/
if (month == 2) {
dayOfMonth += 31;
} else if (month == 3) {
dayOfMonth += 59;
} else if (month == 4) {
dayOfMonth += 90;
} else if (month == 5) {
dayOfMonth += 31 + 28 + 31 + 30;
} else if (month == 6) {
dayOfMonth += 31 + 28 + 31 + 30 + 31;
} else if (month == 7) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30;
} else if (month == 8) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31;
} else if (month == 9) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
} else if (month == 10) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
} else if (month == 11) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
} else if (month == 12) {
dayOfMonth += 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 31;
}
return dayOfMonth;
}
}