I'm planning to create a program in Java that determines if a year entered is a leap year and a valid date. Taking that date I want it to covert to a full written name (4/2/2013 = April 2nd, 2013) and then determine what that day number is in the year (4/2/2013 = Day 92).
There are a lot of programs that do one or another, but I'm learn ways/get ideas on how to combine it all in one if its possible.
To check the leap year, this is what i used:
public class LeapYear {
public static void main (String[] args) {
int theYear;
System.out.print("Enter the year: ");
theYear = Console.in.readInt();
if (theYear < 100) {
if (theYear > 40) {
theYear = theYear + 1900;
}
else {
theYear = theYear + 2000;
}
}
if (theYear % 4 == 0) {
if (theYear % 100 != 0) {
System.out.println(theYear + " is a leap year.");
}
else if (theYear % 400 == 0) {
System.out.println(theYear + " is a leap year.");
}
else {
System.out.println(theYear + " is not a leap year.");
}
}
else {
System.out.println(theYear + " is not a leap year.");
}
}
}
I realize I need to change it a bit to also read the month and day of the year, but for this case, I'm just checking the year. How can I also take that same date entered and convert it to a full written name? Would I have to create an if statement like:
if (theMonth == 4){
System.out.println("April");
if (theDay == 2){
System.out.print(" 2nd, " + theYear + ".");
}
}
That seems like a lot of hardcoded work. I'm trying limit the amount of hardcoding needed so I can get something like:
Output:
Valid entry (4/2/2013).
It is April 2nd, 2013.
It is not a leap year.
It is day 92.
If there is an error, like invalid date, I want the program to reprompt the user until a valid entry is received rather than having to run the program (while writing 'Quit' ends the the program).
I figure I could likely just create different classes for the main method (getting the date), check if its a leap year, a Conversion method, and maybe a validation method.