-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThirtyDaysFunction.java
83 lines (73 loc) · 2.01 KB
/
ThirtyDaysFunction.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
import java.util.Scanner;
/**
* Contains functions that make it easier to work with months.
*/
public class ThirtyDaysFunction {
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-12)
*
* @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 = "Febuary";
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 i the month, or 31 if out 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;
}
}