Skip to content

Instantly share code, notes, and snippets.

@joennlae
Created October 29, 2019 20:30
Show Gist options
  • Save joennlae/548bd69696afbd2c2aff9952caa067ed to your computer and use it in GitHub Desktop.
Save joennlae/548bd69696afbd2c2aff9952caa067ed to your computer and use it in GitHub Desktop.
Selina Day of the year
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");
}
}
}
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 = false;
} else if (year%100 != 0) {
LeapYear = true;
} else if (year%400 != 0) {
LeapYear = false;
} else {
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) {
/*assert day >= 1 && day <= 31 : "Invalid date";
assert month >= 1 && month <= 12 : "Invalid date";
assert year >= 1900 : "Invalid date";
assert isLeapYear(year) == false && month == 2 && day > 29 : "Invalid date";
return true;*/
boolean Valid = false;
if (isLeapYear(year)==false && month == 2 && day > 28){
Out.print("invalid date");
Valid = false;
} else if (day >= 1 && day <= 31 && month >= 1 && month <= 12 && year >= 1900) {
Out.print("invalid date");
Valid = true;
}
return Valid;
/*if (day >= 1 && day <= 31 && month >= 1 && month <= 12 && year >= 1900 || isLeapYear(year) == false && month == 2 && day < 29){
return true;
} else {
Out.print("invalid date");
return false;
}*/
}
/**
* (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;
if (year > 1901){
TotalDays = (year - 1901) * 365 + countTotalLeapYears + countDaysInFullMonth + day;
} else if (year == 1901){
TotalDays = 365 + countDaysInFullMonth + day;
} else {
TotalDays = countDaysInFullMonth + day;
}
return TotalDays;
}
public static void main(String[] args) {
//Out.print("Enter a day between 1 and 31");
int day = In.readInt();
//Out.println("Enter a month between 1 and 12");
int month = In.readInt();
//Out.println("Enter a year greater or equal than 1900");
int year = In.readInt();
boolean isValid = isValidDate( day, month, year );
boolean LeapYear = isLeapYear(year);
int TotalDays = countDays(day, month, year);
if (TotalDays%7 == 0){
Out.print("sunday");
} else if (TotalDays%7 == 1){
Out.print("monday");
} else if (TotalDays%7 == 2){
Out.print("tuesday");
} else if (TotalDays%7 == 3){
Out.print("wednesday");
} else if (TotalDays%7 == 4){
Out.print("thursday");
} else if (TotalDays%7 == 5){
Out.print("friday");
} else {
Out.print("saturday");
}
// Out.print("Enter a number: ");
// int n = In.readInt();
// boolean is2019LeapYear = isLeapYear(2000);
//Out.print(is2019LeapYear);
}
}
@joennlae
Copy link
Author

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 👍

@joennlae
Copy link
Author

Ehm ih hans halt alli Outund In miesse in System.out und System.in miesse umwandle das muesch wieder zruggwandle. Sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment