diff options
Diffstat (limited to 'src/Q6/WeekDay.java')
| -rw-r--r-- | src/Q6/WeekDay.java | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/src/Q6/WeekDay.java b/src/Q6/WeekDay.java index 003fafa..9b322c8 100644 --- a/src/Q6/WeekDay.java +++ b/src/Q6/WeekDay.java @@ -1,5 +1,7 @@ package Q6; +import java.util.*; + public class WeekDay { private int numberOfDays; public static final int JANUARY = 0; @@ -24,7 +26,10 @@ public class WeekDay { public static final int SATURDAY = 0; public String getWeekDay(int day, int month, int year) { - switch (daysSinceEpoch(day, month, year) % 7) { + int totalDaysSinceEpoch = daysSinceEpoch(day, month, year); + this.puts("days since epoch: %d", totalDaysSinceEpoch); + this.puts("weekday: %d", totalDaysSinceEpoch % 7); + switch (totalDaysSinceEpoch % 7) { case 0: return "Sunday"; case 1: @@ -46,10 +51,13 @@ public class WeekDay { private int daysSinceEpoch(int day, int month, int year) { int days = (year - 1900) * 365; + this.puts("days: %d", days); days += (year - 1900) / 4; - if (isLeapYear(year) && (month == 1 || month == 2)) - days -= 1; + this.puts("days: %d", days); + if (isLeapYear(year) && (month == 1 || month == 2)) days -= 1; + this.puts("days: %d", days); days += daysThisYearUpTo(day, month); + this.puts("days: %d", days); return days; } @@ -59,8 +67,10 @@ public class WeekDay { private int daysThisYearUpTo(int day, int month) { int x = 0; - for (int i = 1; i < month; i++) - x += daysInMonth(month); + for (int i = 1; i < month; i++) { + this.puts("days in month: %d is %d", month, daysInMonth(month)); + x += daysInMonth(i); + } return x + day; } @@ -94,4 +104,23 @@ public class WeekDay { return 0; } } + + private void puts(String format, Object... args) { + System.out.println(String.format(format, args)); + } + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + + System.out.println("Enter year:"); + int year = in.nextInt(); + + System.out.println("Enter month:"); + int month = in.nextInt(); + + System.out.println("Enter day:"); + int day = in.nextInt(); + + System.out.println(new WeekDay().getWeekDay(day, month, year)); + } } |
