Created
October 29, 2019 20:30
-
-
Save joennlae/548bd69696afbd2c2aff9952caa067ed to your computer and use it in GitHub Desktop.
Selina Day of the year
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.Scanner; | |
public class Main { | |
/** | |
* @param year a year greater or equal to 1900 | |
* @return whether that year was a leap year | |
*/ | |
static boolean isLeapYear(int year) { | |
// ... | |
boolean LeapYear = false; | |
if (year % 4 == 0) { | |
LeapYear = true; | |
} | |
if (year % 100 == 0) { | |
LeapYear = false; | |
} | |
if (year % 400 == 0) { | |
LeapYear = true; | |
} | |
return LeapYear; | |
} | |
/** | |
* @param month a month between 1 and 12 | |
* @param year a year greater or equal than 1900 | |
* @return number of days in the month of that year | |
*/ | |
static int countDaysInMonth(int month, int year) { | |
int number_Of_DaysInMonth = 0; | |
boolean LeapYear = isLeapYear(year); | |
switch (month) { | |
case 1: | |
// MonthOfName = "January"; | |
number_Of_DaysInMonth = 31; | |
break; | |
case 2: | |
// MonthOfName = "February"; | |
if (LeapYear == true) { | |
number_Of_DaysInMonth = 29; | |
} else { | |
number_Of_DaysInMonth = 28; | |
} | |
break; | |
case 3: | |
// MonthOfName = "March"; | |
number_Of_DaysInMonth = 31; | |
break; | |
case 4: | |
// MonthOfName = "April"; | |
number_Of_DaysInMonth = 30; | |
break; | |
case 5: | |
// MonthOfName = "May"; | |
number_Of_DaysInMonth = 31; | |
break; | |
case 6: | |
// MonthOfName = "June"; | |
number_Of_DaysInMonth = 30; | |
break; | |
case 7: | |
// MonthOfName = "July"; | |
number_Of_DaysInMonth = 31; | |
break; | |
case 8: | |
// MonthOfName = "August"; | |
number_Of_DaysInMonth = 31; | |
break; | |
case 9: | |
// MonthOfName = "September"; | |
number_Of_DaysInMonth = 30; | |
break; | |
case 10: | |
// MonthOfName = "October"; | |
number_Of_DaysInMonth = 31; | |
break; | |
case 11: | |
// MonthOfName = "November"; | |
number_Of_DaysInMonth = 30; | |
break; | |
case 12: | |
// MonthOfName = "December"; | |
number_Of_DaysInMonth = 31; | |
} | |
return number_Of_DaysInMonth; | |
} | |
/** | |
* @param day a day between 1 and 31 | |
* @param month a month between 1 and 12 | |
* @param year a year greater or equal than 1900 | |
* @return whether the date is valid | |
*/ | |
static boolean isValidDate(int day, int month, int year) { | |
if (day < 1 || day > 31) | |
return false; | |
if (month < 1 || month > 12) | |
return false; | |
if (year < 1900) | |
return false; | |
if (day > countDaysInMonth(month, year)) | |
return false; | |
return true; | |
} | |
/** | |
* (the given values should represent a valid date) | |
* | |
* @param day a day between 1 and 31 | |
* @param month a month between 1 and 12 | |
* @param year a year greater or equal than 1900 | |
* @return number of days between Monday January 1, 1900 and this date | |
*/ | |
static int countDays(int day, int month, int year) { | |
int countTotalLeapYears = 0; | |
int countDaysInFullMonth = 0; | |
for (int i = 1900; i < year; i++) { | |
if (isLeapYear(i) == true) { | |
countTotalLeapYears += 1; | |
} | |
} | |
for (int i = 1; i < month; i++) { | |
countDaysInFullMonth += countDaysInMonth(i, year); | |
} | |
int TotalDays = 0; | |
TotalDays = (year - 1900) * 365 + countTotalLeapYears + countDaysInFullMonth + day; | |
return TotalDays; | |
} | |
public static void main(String[] args) throws IOException{ | |
Scanner input = new Scanner(System.in); | |
System.out.print("Enter a day between 1 and 31 \n"); | |
int day = input.nextInt(); | |
System.out.println("Enter a month between 1 and 12\n"); | |
int month = input.nextInt(); | |
System.out.println("Enter a year greater or equal than 1900\n"); | |
int year = input.nextInt(); | |
boolean isValid = isValidDate(day, month, year); | |
assert isValid : "Date not valid"; | |
int TotalDays = countDays(day, month, year); | |
if (TotalDays % 7 == 0) { | |
System.out.print("sunday"); | |
} else if (TotalDays % 7 == 1) { | |
System.out.print("monday"); | |
} else if (TotalDays % 7 == 2) { | |
System.out.print("tuesday"); | |
} else if (TotalDays % 7 == 3) { | |
System.out.print("wednesday"); | |
} else if (TotalDays % 7 == 4) { | |
System.out.print("thursday"); | |
} else if (TotalDays % 7 == 5) { | |
System.out.print("friday"); | |
} else { | |
System.out.print("saturday"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ehm ih hans halt alli
Out
undIn
miesse inSystem.out
undSystem.in
miesse umwandle das muesch wieder zruggwandle. Sorry