-
-
Save joennlae/548bd69696afbd2c2aff9952caa067ed to your computer and use it in GitHub Desktop.
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"); | |
} | |
} | |
} |
bi is valid date wär dini funktion mit de assert au fascht richtig gsi:
s einzig wo falsch gsi isch isch das
assert isLeapYear(year) == false && month == 2 && day > 29 : "Invalid date";
Bi assert muesch immer luege das diä ganzi kondition true isch sunscht bricht s program ab. Do wär er bi jedem Datum wo nid es Schaltjoohr isch und bi jedem Datum wo niid im Februar isch abbroche worde.
Was de guet überleggt hesch isch das de muesch im Februar luege das er niid dörf grösser als 29 si. Aber das muesch au luege wenn er kei Schaltjoohr isch und au bi allne ander Mönet wo nume 30 täg hän.
Ih han das mit dinere Perfekt Funktion countDaysInMonth(month, year)
💯 glöst
if (day > countDaysInMonth(month, year))
return false;
und diä ganz funktion hani no es bitzeli umgschribe das si chliih übersichtlichter isch
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;
}
bi de static int countDays(int day, int month, int year)
isch au wieder fascht alles richtig gsi 👍 bzw. si würdi funktioniere 💯
Jedoch bruchts diä ganze if am schluss nid
if (year > 1901){
TotalDays = (year - 1901) * 365 + countTotalLeapYears + countDaysInFullMonth + day;
} else if (year == 1901){
TotalDays = 365 + countDaysInFullMonth + day;
} else {
TotalDays = countDaysInFullMonth + day;
}
chame eifach schribe als
TotalDays = (year - 1900) * 365 + countTotalLeapYears + countDaysInFullMonth + day;
im public static void main(String[] args)
isch wieder alles tiptop gsi 💯
Im gross ganze super gsi nur es paar chleini fehlerli gmacht. Wiiter so 👍
Ehm ih hans halt alli Out
und In
miesse in System.out
und System.in
miesse umwandle das muesch wieder zruggwandle. Sorry
zweiti Funktion
static int countDaysInMonth(int month, int year)
isch perfekt super 💯 🥇