Java Newbie Question?

WhyKnow

New member
May 24, 2011
0
0
0
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. :(
 
When I run your code and I put

System.out.println(getStartDay(1800, 1));

the output is 3, which I had suspected. The reason is that line 95 equates to

(0 + 3) % 7 = 3.

totalNumberOfDays is 0 because getTotalNumberOfDays returns 0. And that is because control never enters the for loop in getTotalNumberOfDays():

for(int i=1800; i<year; i++),

If the value of the parameter, year, is 1800, then the loop condition is never true. So, it's not that your code is wrong, you just expected a wrong answer. I ran a few test cases on getStartDay() and it appears that it's correct.
 
Java has the GregorianCalendar API which greatly simplifies this as a problem. If you are needing to re-invent the wheel... most implementations use arrays instead of logic trees. Therefore, Jan = index 0, Dec = index 11. The parallel array would then be: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31

The if(month == 1 || will evaluate as the first false and quit the gate. || does not sniff each condition going through the whole possible outcomes. && does, but || does not.

So, it takes a re-write and I don't exactly know what the purpose of the program should be.
 
The string "return (totalNumberOfDays + startDay1800) % 7;"
does not exist in your code. Sorry, I can't help you
 
Back
Top