|
Month |
|
1 // joi/7/bank/class Month
2 //
3 //
4 // Copyright 2003 Bill Campbell and Ethan Bolker
5
6 import java.io.*;
7 import java.util.Calendar;
8
9 /**
10 * The Month class implements an object that keeps
11 * track of the month of the year.
12 *
13 * @version 7
14 */
15
16 public class Month
17 {
18 private static final String[] monthName =
19 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
20 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
21
22 private int month;
23 private int year;
24
25 /**
26 * Month constructor constructs a Month object
27 * initialized to the current month and year.
28 */
29
30 public Month()
31 {
32 Calendar rightNow = Calendar.getInstance();
33 month = rightNow.get( Calendar.MONTH );
34 year = rightNow.get( Calendar.YEAR );
35 }
36
37 /**
38 * Advance to next month.
39 */
40
41 public void next()
42 {
43 month = (month + 1) % 12;
44 if (month == 0) {
45 year++;
46 }
47 }
48
49 /**
50 * How a Month is displayed as a String -
51 * for example, "Jan, 2003".
52 *
53 * @return String representation of the month.
54 */
55
56 public String toString()
57 {
58 return monthName[month] + ", " + year;
59 }
60
61 /**
62 * For unit testing.
63 */
64
65 public static void main( String[] args )
66 {
67 Month m = new Month();
68 for (int i=0; i < 14; i++, m.next()) {
69 System.out.println(m);
70 }
71 for (int i=0; i < 35; i++, m.next()); // no loop body
72 System.out.println( "three years later: " + m );
73 for (int i=0; i < 120; i++, m.next());// no loop body
74 System.out.println( "ten years later: " + m );
75 }
76 }
77
|
Month |
|