Hi. I have another question about this program.
I think I have figured it out, just need someone to validate my theory.
Line 081System.out.println(getNumberOfDaysInMonth(year, month));
Line 083System.out.println(startDay);
Line 081 = 31 Line 083 =3, If I use year = 1800, month = 1 as my input.
Dose that mean that a Method can only refer to no more the a single child method
startDay called for ---> getTotalNumberOfDays on Line 092 ;This returned 3
getTotalNumberOfDays called for ---> getNumberOfDaysInMonth on Line 109; This returned 31
if it was able to refer to more then a single child method wouldn't it be like the following
startDay called for ---> getTotalNumberOfDays --->getNumberOfDAysInMonth; should return 6
because
Line 094 return (totalNumberOfDays + startDay1800)%7;
if it was able to refer to more then a single child method wouldn't line 094 equate to:
return (31 + 3)%7 = 6
instead of
return (0 + 3)%7 = 3
001 import javax.swing.JOptionPane;
002
003 public class PrintCalendar
004 {
005public static void main (String[]args)
006{
007
008String strMonth = JOptionPane.showInputDialog(null,"Please enter the
009 month","MONTH",JOptionPane.QUESTION_MESSAGE);
010 int month = Integer.parseInt(strMonth);
011
012String strYear = JOptionPane.showInputDialog(null,"Please enter a
013 year","YEAR",JOptionPane.QUESTION_MESSAGE);
014int year = Integer.parseInt(strYear);
015
016printMonth(year,month);
017 }
018 //Run - print Title and Body
019static void printMonth(int year, int month)
020{
021printMonthTitle(year, month);
022
023printMonthBody (year, month);
024}
025//Run - Title Print
026 static void printMonthTitle(int year, int month)
027{
028System.out.println(" " + getMonthName(month) + year);
029System.out.println("----------------------------");
030System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
031}
032//Validate - Month String
033static String getMonthName(int month)
034{
035String monthName = null;
036switch (month){
037case 1: monthName = "January"; break;
038case 2: monthName = "February"; break;
039case 3: monthName = "March"; break;
040case 4: monthName = "April"; break;
041case 5: monthName = "May"; break;
042case 6: monthName = "June"; break;
043case 7: monthName = "July"; break;
044case 8: monthName = "Ausgust"; break;
045case 9: monthName = "September"; break;
046case 10: monthName = "October"; break;
047case 11: monthName = "November"; break;
048case 12: monthName = "December"; break;
049}
050return monthName;
051}
052//Run - Body Print
053static void printMonthBody(int year,int month)
054{
055int startDay = getStartDay(year, month);
056int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
057
058
059for(int i=0; i<startDay; i++)
060{
067System.out.print(" ");
068}
069for(int i=1; i<=getNumberOfDaysInMonth(year, month); i++)
070{
071if (i<10)
072System.out.print(" " + i);
073else
074System.out.print(" " + i);
075
076if ((i + startDay)%7 == 0)
077System.out.println();
078}
079System.out.println();
080
081System.out.println(getNumberOfDaysInMonth(year, month));
082
083System.out.println(startDay);
084}
085//Validate - which day the date starts on.
086static int getStartDay(int year, int month)
087{
088//On the 1/1/1800 starts on a Wednesday 3 days away from Sunday
089int startDay1800 = 3;
090
091
092int totalNumberOfDays = getTotalNumberOfDays(year, month);
093
094 return (totalNumberOfDays + startDay1800)%7;
095}
096//Validate - the total number of days in the year and month
097static int getTotalNumberOfDays(int year, int month)
098{
099int totalDays = 0;
100//Add 366 or 365 days if it is more then 1800 year
101for(int i=1800; i<year; i++)
102if (isLeapYear(i))
103totalDays = totalDays + 366;
104else
105totalDays = totalDays + 365;
106
107//add the days in as each month rows by
108for(int i=1; i<month; i++)
109totalDays = totalDays + getNumberOfDaysInMonth(year, i);
110
111return totalDays;
112}
113//Validate - the days for each of the month
114static int getNumberOfDaysInMonth(int year, int month)
115{
116if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||
117 month==12)
118return 31;
119if(month==4 || month==6 || month==9 || month==11)
120return 30;
121if(month==2)
122return isLeapYear(year)? 29:28;
123else
124return 0;
125
I think I have figured it out, just need someone to validate my theory.
Line 081System.out.println(getNumberOfDaysInMonth(year, month));
Line 083System.out.println(startDay);
Line 081 = 31 Line 083 =3, If I use year = 1800, month = 1 as my input.
Dose that mean that a Method can only refer to no more the a single child method
startDay called for ---> getTotalNumberOfDays on Line 092 ;This returned 3
getTotalNumberOfDays called for ---> getNumberOfDaysInMonth on Line 109; This returned 31
if it was able to refer to more then a single child method wouldn't it be like the following
startDay called for ---> getTotalNumberOfDays --->getNumberOfDAysInMonth; should return 6
because
Line 094 return (totalNumberOfDays + startDay1800)%7;
if it was able to refer to more then a single child method wouldn't line 094 equate to:
return (31 + 3)%7 = 6
instead of
return (0 + 3)%7 = 3
001 import javax.swing.JOptionPane;
002
003 public class PrintCalendar
004 {
005public static void main (String[]args)
006{
007
008String strMonth = JOptionPane.showInputDialog(null,"Please enter the
009 month","MONTH",JOptionPane.QUESTION_MESSAGE);
010 int month = Integer.parseInt(strMonth);
011
012String strYear = JOptionPane.showInputDialog(null,"Please enter a
013 year","YEAR",JOptionPane.QUESTION_MESSAGE);
014int year = Integer.parseInt(strYear);
015
016printMonth(year,month);
017 }
018 //Run - print Title and Body
019static void printMonth(int year, int month)
020{
021printMonthTitle(year, month);
022
023printMonthBody (year, month);
024}
025//Run - Title Print
026 static void printMonthTitle(int year, int month)
027{
028System.out.println(" " + getMonthName(month) + year);
029System.out.println("----------------------------");
030System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
031}
032//Validate - Month String
033static String getMonthName(int month)
034{
035String monthName = null;
036switch (month){
037case 1: monthName = "January"; break;
038case 2: monthName = "February"; break;
039case 3: monthName = "March"; break;
040case 4: monthName = "April"; break;
041case 5: monthName = "May"; break;
042case 6: monthName = "June"; break;
043case 7: monthName = "July"; break;
044case 8: monthName = "Ausgust"; break;
045case 9: monthName = "September"; break;
046case 10: monthName = "October"; break;
047case 11: monthName = "November"; break;
048case 12: monthName = "December"; break;
049}
050return monthName;
051}
052//Run - Body Print
053static void printMonthBody(int year,int month)
054{
055int startDay = getStartDay(year, month);
056int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
057
058
059for(int i=0; i<startDay; i++)
060{
067System.out.print(" ");
068}
069for(int i=1; i<=getNumberOfDaysInMonth(year, month); i++)
070{
071if (i<10)
072System.out.print(" " + i);
073else
074System.out.print(" " + i);
075
076if ((i + startDay)%7 == 0)
077System.out.println();
078}
079System.out.println();
080
081System.out.println(getNumberOfDaysInMonth(year, month));
082
083System.out.println(startDay);
084}
085//Validate - which day the date starts on.
086static int getStartDay(int year, int month)
087{
088//On the 1/1/1800 starts on a Wednesday 3 days away from Sunday
089int startDay1800 = 3;
090
091
092int totalNumberOfDays = getTotalNumberOfDays(year, month);
093
094 return (totalNumberOfDays + startDay1800)%7;
095}
096//Validate - the total number of days in the year and month
097static int getTotalNumberOfDays(int year, int month)
098{
099int totalDays = 0;
100//Add 366 or 365 days if it is more then 1800 year
101for(int i=1800; i<year; i++)
102if (isLeapYear(i))
103totalDays = totalDays + 366;
104else
105totalDays = totalDays + 365;
106
107//add the days in as each month rows by
108for(int i=1; i<month; i++)
109totalDays = totalDays + getNumberOfDaysInMonth(year, i);
110
111return totalDays;
112}
113//Validate - the days for each of the month
114static int getNumberOfDaysInMonth(int year, int month)
115{
116if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||
117 month==12)
118return 31;
119if(month==4 || month==6 || month==9 || month==11)
120return 30;
121if(month==2)
122return isLeapYear(year)? 29:28;
123else
124return 0;
125