Post by Jonathan on Feb 21, 2006 11:03:23 GMT -5
// ghaderps@vaniercollege.qc.ca
// Prince, Jonathan
// Prompts for and reads a date as well as outputs the number of days from
// the beginning of the year and and the date of the next day
import java.util.Scanner;
public class LeapYears
{
public static void main(String[] args)
{
/****************************/
int month;
int day;
int year;
final int JAN = 31;
final int FEBLEAP = 28;
final int FEBNONLEAP = 29;
int FEB;
final int MAR = 31;
final int APRIL = 30;
final int MAY = 31;
final int JUNE = 30;
final int JULY = 31;
final int AUG = 31;
final int SEPT = 30;
final int OCT = 31;
final int NOV = 30;
final int DEC = 31;
final int THIRTYDAYS = 30;
final int THIRTYONEDAYS = 31;
boolean isLeapYear;
/****************************/
/****Get input****/
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a date: (year month day): ");
year = keyboard.nextInt();
month = keyboard.nextInt();
day = keyboard.nextInt();
/*****************/
/****Validate year****/
if (year < 1583)
{
System.out.println("ERROR: The year entered is invalid.");
System.exit(0); // Terminate program
System.out.println("The year is: " + year);
}
/*********************/
/****Is it a leap year?****/
isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
/****Determine days in Feb.****/
if (isLeapYear)
{
FEB = 29;
}
else
{
FEB = 28;
}
/******************************/
/****Validate month****/
if( (month < 1) || (month > 12) )
{
System.out.println("ERROR: The month entered is invalid.");
System.exit(0); // Terminate program
}
/**********************/
/****Validate day****/
boolean isValidDay = true; // initial assumption
switch(month)
{
case 2:
isValidDay = (day >=1) && (day <= FEB) ;
break;
case 4: case 6: case 9: case 11:
isValidDay = (day >=1) && (day <= THIRTYDAYS) ;
break;
default:
isValidDay = (day >=1) && (day <= THIRTYONEDAYS) ;
break;
}
if( ! isValidDay )
{
System.out.println("ERROR: The day entered is invalid.");
System.exit(0); // Terminate program
}
/********************/
/****Get Month's name****/
String monthName = "Unknown";
int monthDays = 0;
switch (month)
{
case 1: monthName = "January";
monthDays = JAN;
if(day>=JAN) monthName = "February";
break;
case 2: monthName = "February";
monthDays = FEB;
if(day>=FEB) monthName = "March";
break;
case 3: monthName = "March";
monthDays = MAR;
if(day>=MAR) monthName = "April";
break;
case 4: monthName = "April";
monthDays = APRIL;
if(day>=APRIL) monthName = "May";
break;
case 5: monthName = "May";
monthDays = MAY;
if(day>=MAY) monthName = "June";
break;
case 6: monthName = "June";
monthDays = JUNE;
if(day>=JUNE) monthName = "July";
break;
case 7: monthName = "July";
monthDays = JULY;
if(day>=JULY) monthName = "August";
break;
case 8: monthName = "August";
monthDays = AUG;
if(day>=AUG) monthName = "September";
break;
case 9: monthName = "September";
monthDays = SEPT;
if(day>=SEPT) monthName = "October";
break;
case 10: monthName = "October";
monthDays = OCT;
if(day>=OCT) monthName = "November";
break;
case 11: monthName = "November";
monthDays = NOV;
if(day>=NOV) monthName = "December";
break;
case 12: monthName = "December";
monthDays = DEC;
if(day>=DEC) monthName = "January";
break;
} /****Closes switch****/
/****Outputs the Date****/
System.out.println("The date is: " + monthName + " " + day + ", " + year + ".");
/****Discerning the day's position of the year.****/
int totalDays = 0;
if(month == 1)
totalDays = day;
else if(month == 2)
totalDays = JAN + day;
else if(month == 3)
totalDays = JAN + FEB + day;
else if(month == 4)
totalDays = JAN + FEB + MAR + day;
else if(month == 5)
totalDays = JAN + FEB + MAR + APRIL + day;
else if(month == 6)
totalDays = JAN + FEB + MAR + APRIL + MAY + day;
else if(month == 7)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + day;
else if(month == 8)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + day;
else if(month == 9)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + AUG + day;
else if(month == 10)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + AUG + SEPT + day;
else if(month == 11)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + AUG + SEPT + OCT + day;
else if(month == 12)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + AUG + SEPT + OCT + NOV + day;
/**************************************************/
/****Outputs the number of days that have passed so far****/
System.out.println(totalDays + " days have passed so far this year. ");
/****Find the next Day****/
int nextDay = 0;
nextDay = day + 1;
switch(month)
{
case 2:
if(day >= FEB)
nextDay = 1;
month++;
if( month > 12)
{
month = 1;
year++;
}
case 4: case 6: case 9: case 11:
if(day == 30)
nextDay = 1;
month++;
if( month > 12)
{
month = 1;
year++;
}
default:
if(day == 31)
nextDay = 1;
month++;
if( month > 12)
{
month = 1;
year++;
}
}/****close switch****/
/****Outputs the next day's date.****/
System.out.println("The next day is: " + monthName + " " + nextDay + ", " + year + ".");
} // main
} // class LeapYears
// Prince, Jonathan
// Prompts for and reads a date as well as outputs the number of days from
// the beginning of the year and and the date of the next day
import java.util.Scanner;
public class LeapYears
{
public static void main(String[] args)
{
/****************************/
int month;
int day;
int year;
final int JAN = 31;
final int FEBLEAP = 28;
final int FEBNONLEAP = 29;
int FEB;
final int MAR = 31;
final int APRIL = 30;
final int MAY = 31;
final int JUNE = 30;
final int JULY = 31;
final int AUG = 31;
final int SEPT = 30;
final int OCT = 31;
final int NOV = 30;
final int DEC = 31;
final int THIRTYDAYS = 30;
final int THIRTYONEDAYS = 31;
boolean isLeapYear;
/****************************/
/****Get input****/
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a date: (year month day): ");
year = keyboard.nextInt();
month = keyboard.nextInt();
day = keyboard.nextInt();
/*****************/
/****Validate year****/
if (year < 1583)
{
System.out.println("ERROR: The year entered is invalid.");
System.exit(0); // Terminate program
System.out.println("The year is: " + year);
}
/*********************/
/****Is it a leap year?****/
isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
/****Determine days in Feb.****/
if (isLeapYear)
{
FEB = 29;
}
else
{
FEB = 28;
}
/******************************/
/****Validate month****/
if( (month < 1) || (month > 12) )
{
System.out.println("ERROR: The month entered is invalid.");
System.exit(0); // Terminate program
}
/**********************/
/****Validate day****/
boolean isValidDay = true; // initial assumption
switch(month)
{
case 2:
isValidDay = (day >=1) && (day <= FEB) ;
break;
case 4: case 6: case 9: case 11:
isValidDay = (day >=1) && (day <= THIRTYDAYS) ;
break;
default:
isValidDay = (day >=1) && (day <= THIRTYONEDAYS) ;
break;
}
if( ! isValidDay )
{
System.out.println("ERROR: The day entered is invalid.");
System.exit(0); // Terminate program
}
/********************/
/****Get Month's name****/
String monthName = "Unknown";
int monthDays = 0;
switch (month)
{
case 1: monthName = "January";
monthDays = JAN;
if(day>=JAN) monthName = "February";
break;
case 2: monthName = "February";
monthDays = FEB;
if(day>=FEB) monthName = "March";
break;
case 3: monthName = "March";
monthDays = MAR;
if(day>=MAR) monthName = "April";
break;
case 4: monthName = "April";
monthDays = APRIL;
if(day>=APRIL) monthName = "May";
break;
case 5: monthName = "May";
monthDays = MAY;
if(day>=MAY) monthName = "June";
break;
case 6: monthName = "June";
monthDays = JUNE;
if(day>=JUNE) monthName = "July";
break;
case 7: monthName = "July";
monthDays = JULY;
if(day>=JULY) monthName = "August";
break;
case 8: monthName = "August";
monthDays = AUG;
if(day>=AUG) monthName = "September";
break;
case 9: monthName = "September";
monthDays = SEPT;
if(day>=SEPT) monthName = "October";
break;
case 10: monthName = "October";
monthDays = OCT;
if(day>=OCT) monthName = "November";
break;
case 11: monthName = "November";
monthDays = NOV;
if(day>=NOV) monthName = "December";
break;
case 12: monthName = "December";
monthDays = DEC;
if(day>=DEC) monthName = "January";
break;
} /****Closes switch****/
/****Outputs the Date****/
System.out.println("The date is: " + monthName + " " + day + ", " + year + ".");
/****Discerning the day's position of the year.****/
int totalDays = 0;
if(month == 1)
totalDays = day;
else if(month == 2)
totalDays = JAN + day;
else if(month == 3)
totalDays = JAN + FEB + day;
else if(month == 4)
totalDays = JAN + FEB + MAR + day;
else if(month == 5)
totalDays = JAN + FEB + MAR + APRIL + day;
else if(month == 6)
totalDays = JAN + FEB + MAR + APRIL + MAY + day;
else if(month == 7)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + day;
else if(month == 8)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + day;
else if(month == 9)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + AUG + day;
else if(month == 10)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + AUG + SEPT + day;
else if(month == 11)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + AUG + SEPT + OCT + day;
else if(month == 12)
totalDays = JAN + FEB + MAR + APRIL + MAY + JUNE + JULY + AUG + SEPT + OCT + NOV + day;
/**************************************************/
/****Outputs the number of days that have passed so far****/
System.out.println(totalDays + " days have passed so far this year. ");
/****Find the next Day****/
int nextDay = 0;
nextDay = day + 1;
switch(month)
{
case 2:
if(day >= FEB)
nextDay = 1;
month++;
if( month > 12)
{
month = 1;
year++;
}
case 4: case 6: case 9: case 11:
if(day == 30)
nextDay = 1;
month++;
if( month > 12)
{
month = 1;
year++;
}
default:
if(day == 31)
nextDay = 1;
month++;
if( month > 12)
{
month = 1;
year++;
}
}/****close switch****/
/****Outputs the next day's date.****/
System.out.println("The next day is: " + monthName + " " + nextDay + ", " + year + ".");
} // main
} // class LeapYears