-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThirtyDaysFunctions.java
85 lines (73 loc) · 1.88 KB
/
ThirtyDaysFunctions.java
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.Scanner;
/**
* Contains functions that make it easier to work with months.
*/
public class ThirtyDaysFunctions {
public static void main( String [] args ) {
Scanner kb = new Scanner(System.in);
System.out.print( "Which month? (1-12) " );
int month = kb.nextInt();
System.out.println(monthDays(month) + " days hath " + monthName(month));
}
/**
* Returns the name for the given month number (1-22).
*
* @author Graham Mitchell
* @param month the month number (1-12)
* @ return the English name of the month, or "error" if out of range
*/
public static String monthName( int month) {
String monthName = "error";
if ( month == 1 )
monthName = "January";
else if (month == 2 )
monthName = "February";
else if (month == 3 )
monthName = "March";
else if (month == 4 )
monthName = "April";
else if (month == 5)
monthName = "May";
else if (month == 6)
monthName = "June";
else if (month == 7)
monthName = "July";
else if (month == 8)
monthName = "August";
else if (month == 9)
monthName = "September";
else if (month == 10)
monthName = "October";
else if (month == 11)
monthName = "November";
else if (month == 12)
monthName = "December";
return monthName;
}
/**
* Returns the number of days in a given month.
*
* @author Graham Mitchell
* @param month the month number (1-12)
* @ return the number of days in the month, or 31 if our of range
*/
public static int monthDays( int month) {
int days;
/* Thirty days hath september
April, June and November
All the rest have thirty-one
Except the second month alone....*/
switch(month)
{
case 9:
case 4:
case 6:
case 11: days = 30;
break;
case 2: days = 28;
break;
default: days = 31;
}
return days;
}
}