1. Newbies
Join Date
May 2011
Posts
1
Rep Power
0

## NewbieCalendarQuestion

I understand most everything in this program,
but there is one bit that's driving me crazy.

Let's say my inputs are year=1800, month=1
On line 95 ---> return (totalNumberOfDays + startDay1800) % 7;
which means (31 + 3) % 7 = 6

On line 72 ---> for (i = 0; i < startDay; i++)
so doesn't this mean for (i=0; i<6; i++)

How can the remainder be 6. I must be reading it wrong but how:confused:

Java Code:
``` 1  import javax.swing.JOptionPane;
2
3  public class PrintCalendar {
4    /** Main method */
5    public static void main(String[] args) {
6      // Prompt the user to enter year
7      String yearString = JOptionPane.showInputDialog(
8        "Enter full year (e.g., 2001):");
9
10      // Convert string into integer
11      int year = Integer.parseInt(yearString);
12
13      // Prompt the user to enter month
14      String monthString = JOptionPane.showInputDialog(
15        "Enter month in number between 1 and 12:");
16
17      // Convert string into integer
18      int month = Integer.parseInt(monthString);
19
20      // Print calendar for the month of the year
21      printMonth(year, month);
22    }
23
24    /** Print the calendar for a month in a year */
25    static void printMonth(int year, int month) {
26      // Print the headings of the calendar
27      printMonthTitle(year, month);
28
--------------------------------------------------------------------------------[Page 152] 29      // Print the body of the calendar
30      printMonthBody(year, month);
31    }
32
33    /** Print the month title, e.g., May, 1999 */
34    static void printMonthTitle(int year, int month) {
35      System.out.println("         " + getMonthName(month)
36        + " " + year);
37      System.out.println
("–––––––––––––––––––––––––––––");
38      System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
39    }
40
41    /** Get the English name for the month */
42    static String getMonthName(int month) {
43      String monthName = null;
44      switch (month) {
45        case 1: monthName = "January"; break;
46        case 2: monthName = "February"; break;
47        case 3: monthName = "March"; break;
48        case 4: monthName = "April"; break;
49        case 5: monthName = "May"; break;
50        case 6: monthName = "June"; break;
51        case 7: monthName = "July"; break;
52        case 8: monthName = "August"; break;
53        case 9: monthName = "September"; break;
54        case 10: monthName = "October"; break;
55        case 11: monthName = "November"; break;
56        case 12: monthName = "December";
57      }
58
59      return monthName;
60    }
61
62    /** Print month body */
63    static void printMonthBody(int year, int month) {
64      // Get start day of the week for the first date in the month
65      int startDay = getStartDay(year, month);
66
67      // Get number of days in the month
68      int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month) ;
69
70      // Pad space before the first day of the month
71      int i = 0;
72      for (i = 0; i < startDay; i++)
73        System.out.print("    ");
74
75      for (i = 1; i <= numberOfDaysInMonth; i++) {
76        if (i < 10)
77          System.out.print("   " + i);
78        else
79          System.out.print("  " + i);
80
81        if ((i + startDay) % 7 == 0)
82          System.out.println();
83      }
84
85      System.out.println();
86    }
87
88    /** Get the start day of the first day in a month */
89    static int getStartDay(int year, int month) {
--------------------------------------------------------------------------------[Page 153] 90      // Get total number of days since 1/1/1800
91      int startDay1800 = 3;
92      int totalNumberOfDays = getTotalNumberOfDays(year, month);
93
94      // Return the start day
95      return (totalNumberOfDays + startDay1800) % 7;
96    }
97
98    /** Get the total number of days since January 1, 1800 */
99    static int getTotalNumberOfDays(int year, int month) {
100      int total = 0;
101
102      // Get the total days from 1800 to year - 1
103      for (int i = 1800; i < year; i++)
104      if (isLeapYear(i))
105        total = total + 366;
106      else
107        total = total + 365;
108
109      // Add days from January to the month prior to the calendar month
110      for (int i = 1; i < month; i++)
111        total = total + getNumberOfDaysInMonth(year, i);
112
114    }
115
116    /** Get the number of days in a month */
117    static int getNumberOfDaysInMonth(int year, int month) {
118      if (month == 1 || month == 3 || month == 5 || month == 7 ||
119        month == 8 || month == 10 || month == 12)
120        return 31;
121
122      if (month == 4 || month == 6 || month == 9 || month == 11)
123        return 30;
124
125      if (month == 2) return isLeapYear(year) ? 29 : 28;
126
127      return 0; // If month is incorrect
128    }
129
130    /** Determine if it is a leap year */
131    static boolean isLeapYear(int year) {
132      return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
133    }
134  }```
Last edited by Fubarable; 05-22-2011 at 10:13 PM. Reason: code tags added

2. Note: I have added code tags to your post so your code is readable, but you'll want to edit your original post to get rid of the line numbers.

3. That appears to be copyrighted material from introduction to java programming comprehensive version 6th edition. Do you have the right to post it on a public forum?

If not, please edit your post and remove the code. You can post your own simple example to ask for help with anything you don't understand.

db

4. Senior Member
Join Date
Mar 2011
Posts
261
Rep Power
4
Originally Posted by DarrylBurke
That appears to be copyrighted material from introduction to java programming comprehensive version 6th edition. Do you have the right to post it on a public forum?

If not, please edit your post and remove the code. You can post your own simple example to ask for help with anything you don't understand.

db
I highly doubt he has the right to it...

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•