jeudi 5 mars 2015

I need another pair of eye to tell me where I am being dumb again (about calculations)

I am actually trying to do something like calculating the day of the week by knowing the date e.g.February 20, 1950 which is a Monday. Of course there are lots other concerns and I do have the formula already. The thing is somehow when I added an if statement that I am suppose to get let's say 3 and I got 2, if I should get 0 I get -1. Can someone please give me an eye to see what's wrong?


These are the month code variables which will be the output I need and this is the part where the output keep on getting the wrong output than the expected.



/* Month Code */
private static int JAN_CODE = 1;
private static int FEB_CODE = 4;
private static int MAR_CODE = 4;
private static int APR_CODE = 0;
private static int MAY_CODE = 2;
private static int JUN_CODE = 5;
private static int JUL_CODE = 0;
private static int AUG_CODE = 3;
private static int SEP_CODE = 6;
private static int OCT_CODE = 1;
private static int NOV_CODE = 4;
private static int DEC_CODE = 6;


leap year calculation and returns true or false



private boolean isLeapYear(){
if((year % 100 == 0) && (year % 400 != 0))
{
return false;
}else if(year % 4 == 0)
{
return true;
}else{
return false;
}
}


this is the part where to return the monthCode depending on what the month is in integer



public int getMonthCode(){
// variable to store the code
int monthCode;

// January and February dates in leap years: subtract 1
boolean i = isLeapYear();
if (i == true){
JAN_CODE -= 1;
FEB_CODE -= 1;
}

// Check which month uses which month code
switch (month){
case JANUARY_INT: monthCode = JAN_CODE;
break;
case FEBURARY_INT: monthCode = FEB_CODE;
break;
case MARCH_INT: monthCode = MAR_CODE;
break;
case APRIL_INT: monthCode = APR_CODE;
break;
case MAY_INT: monthCode = MAY_CODE;
break;
case JUNE_INT: monthCode = JUN_CODE;
break;
case JULY_INT: monthCode = JUL_CODE;
break;
case AUGUST_INT: monthCode = AUG_CODE;
break;
case SEPTEMBER_INT: monthCode = SEP_CODE;
break;
case OCTOBER_INT: monthCode = OCT_CODE;
break;
case NOVEMBER_INT: monthCode = NOV_CODE;
break;
case DECEMBER_INT: monthCode = DEC_CODE;
break;
default: monthCode = 0;
}
return monthCode;
}


I tried using JAN_CODE = JAN_CODE - 1; which still gets the wrong output. So let's say if January is entered, the JAN_CODE I am getting would be 1 if not a leap year but if it is a leap year it should be 0 but after I put in the if(i == true) statement to see if it is leap year and January is entered, the monthCode I get is -1, if February I get 2 but I am suppose to get 0 for Jan and 3 for Feb isn't it? If I take out the if statement then what is returned is 1 for Jan and 4 for Feb. Also, if I tried a year that's not leap year my return is still deducted by 2.


Can someone give me a hand?


Aucun commentaire:

Enregistrer un commentaire