Java Reference
In-Depth Information
LISTING 8.1
The Full Text of HolidaySked.java
1: import java.util.*;
2:
3: public class HolidaySked {
4: BitSet sked;
5:
6: public HolidaySked() {
7: sked = new BitSet(365);
8: int[] holiday = { 1, 15, 50, 148, 185, 246,
9: 281, 316, 326, 359 };
10: for (int i = 0; i < holiday.length; i++) {
11: addHoliday(holiday[i]);
12: }
13: }
14:
15: public void addHoliday(int dayToAdd) {
16: sked.set(dayToAdd);
17: }
18:
19: public boolean isHoliday(int dayToCheck) {
20: boolean result = sked.get(dayToCheck);
21: return result;
22: }
23:
24: public static void main(String[] arguments) {
25: HolidaySked cal = new HolidaySked();
26: if (arguments.length > 0) {
27: try {
28: int whichDay = Integer.parseInt(arguments[0]);
29: if (cal.isHoliday(whichDay)) {
30: System.out.println(“Day number “ + whichDay +
31: “ is a holiday.”);
32: } else {
33: System.out.println(“Day number “ + whichDay +
34: “ is not a holiday.”);
35: }
36: } catch (NumberFormatException nfe) {
37: System.out.println(“Error: “ + nfe.getMessage());
38: }
39: }
40: }
41: }
8
The HolidaySked class contains only one instance variable: sked , a BitSet that holds
values for each day in a year.
The constructor of the class creates the sked bit set with 365 positions (lines 6-13). All
bit sets are filled with 0 values when they are created.
 
Search WWH ::




Custom Search