76. Computing pregnancy due date
Let’s start with these two constants:
public static final int PREGNANCY_WEEKS = 40;
public static final int PREGNANCY_DAYS = PREGNANCY_WEEKS * 7;
Let’s consider the first day as a LocalDate
and we want to write a calculator that prints the pregnancy due date, the number of remaining days, the number of passed days, and the current week.
Basically, the pregnancy due date is obtained by adding the PREGNANCY_DAYS
to the given first day. Further, the number of remaining days is the difference between today and the given first day, while the number of passed days is PREGNANCY_DAYS
minus the number of remaining days. Finally, the current week is obtained as the number of passed days divided by 7 (since a week has 7 days). Based on these statements, the code speaks for itself:
public static void pregnancyCalculator(LocalDate firstDay) {
firstDay = firstDay.plusDays(PREGNANCY_DAYS);
System.out.println("Due date: "...