Please help me out.
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
import javax.swing.JOptionPane;
public class PrintCalendar
{
public static void main (String[]args)
{
String strMonth = JOptionPane.showInputDialog(null,"Please enter the month","MONTH",JOptionPane.QUESTION_MESSAGE);
int month = Integer.parseInt(strMonth);
String strYear = JOptionPane.showInputDialog(null,"Please enter a year","YEAR",JOptionPane.QUESTION_MESSAGE);
int year = Integer.parseInt(strYear);
printMonth(year,month);
}
//Run - print Title and Body
static void printMonth(int year, int month)
{
printMonthTitle(year, month);
printMonthBody (year, month);
}
//Run - Title Print
static void printMonthTitle(int year, int month)
{
System.out.println(" " + getMonthName(month) + year);
System.out.println("----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
//Validate - Month String
static String getMonthName(int month)
{
String monthName = null;
switch (month){
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "Ausgust"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
}
return monthName;
}
//Run - Body Print
static void printMonthBody(int year,int month)
{
int startDay = getStartDay(year, month);
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
for(int i=0; i<startDay; i++)
{
System.out.print(" ");
}
for(int i=1; i<=getNumberOfDaysInMonth(year, month); i++)
{
if (i<10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay)%7 == 0)
System.out.println();
}
}
//Validate - which day the date starts on.
static int getStartDay(int year, int month)
{
//On the 1/1/1800 starts on a Wednesday 3 days away from Sunday
int startDay1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);
//if the total "number of days + startDay1800 in this case is 3 days" is divisible by seven
return (totalNumberOfDays + startDay1800)%7;
}
//Validate - the total number of days in the year and month
static int getTotalNumberOfDays(int year, int month)
{
int totalDays = 0;
//Add 366 or 365 days if it is more then 1800 year
for(int i=1800; i<year; i++)
if (isLeapYear(i))
totalDays = totalDays + 366;
else
totalDays = totalDays + 365;
//add the days in as each month rows by
for(int i=1; i<month; i++)
totalDays = totalDays + getNumberOfDaysInMonth(year, i);
return totalDays;
}
//Validate - the days for each of the month
static int getNumberOfDaysInMonth(int year, int month)
{
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
return 31;
if(month==4 || month==6 || month==9 || month==11)
return 30;
if(month==2)
return isLeapYear(year)? 29:28;
else
return 0;
}
//Testing - Leap Year ? True or False
static boolean isLeapYear(int year)
{
return year % 400 == 0 || (year % 4 == 0 && year % 100 !=0);
}
}
Thanks for the answers guys
It still does not answer my question.
Why I worked out the remainder to be 6 for startDay, which would make 1/1/1800 print on Sunday.
However, the calendar is still printing on the right day which is Wednesday.
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
import javax.swing.JOptionPane;
public class PrintCalendar
{
public static void main (String[]args)
{
String strMonth = JOptionPane.showInputDialog(null,"Please enter the month","MONTH",JOptionPane.QUESTION_MESSAGE);
int month = Integer.parseInt(strMonth);
String strYear = JOptionPane.showInputDialog(null,"Please enter a year","YEAR",JOptionPane.QUESTION_MESSAGE);
int year = Integer.parseInt(strYear);
printMonth(year,month);
}
//Run - print Title and Body
static void printMonth(int year, int month)
{
printMonthTitle(year, month);
printMonthBody (year, month);
}
//Run - Title Print
static void printMonthTitle(int year, int month)
{
System.out.println(" " + getMonthName(month) + year);
System.out.println("----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
//Validate - Month String
static String getMonthName(int month)
{
String monthName = null;
switch (month){
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "Ausgust"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
}
return monthName;
}
//Run - Body Print
static void printMonthBody(int year,int month)
{
int startDay = getStartDay(year, month);
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
for(int i=0; i<startDay; i++)
{
System.out.print(" ");
}
for(int i=1; i<=getNumberOfDaysInMonth(year, month); i++)
{
if (i<10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay)%7 == 0)
System.out.println();
}
}
//Validate - which day the date starts on.
static int getStartDay(int year, int month)
{
//On the 1/1/1800 starts on a Wednesday 3 days away from Sunday
int startDay1800 = 3;
int totalNumberOfDays = getTotalNumberOfDays(year, month);
//if the total "number of days + startDay1800 in this case is 3 days" is divisible by seven
return (totalNumberOfDays + startDay1800)%7;
}
//Validate - the total number of days in the year and month
static int getTotalNumberOfDays(int year, int month)
{
int totalDays = 0;
//Add 366 or 365 days if it is more then 1800 year
for(int i=1800; i<year; i++)
if (isLeapYear(i))
totalDays = totalDays + 366;
else
totalDays = totalDays + 365;
//add the days in as each month rows by
for(int i=1; i<month; i++)
totalDays = totalDays + getNumberOfDaysInMonth(year, i);
return totalDays;
}
//Validate - the days for each of the month
static int getNumberOfDaysInMonth(int year, int month)
{
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
return 31;
if(month==4 || month==6 || month==9 || month==11)
return 30;
if(month==2)
return isLeapYear(year)? 29:28;
else
return 0;
}
//Testing - Leap Year ? True or False
static boolean isLeapYear(int year)
{
return year % 400 == 0 || (year % 4 == 0 && year % 100 !=0);
}
}
Thanks for the answers guys
It still does not answer my question.
Why I worked out the remainder to be 6 for startDay, which would make 1/1/1800 print on Sunday.
However, the calendar is still printing on the right day which is Wednesday.