Write a method named monthApart that accepts four integer parameters representin
ID: 3648989 • Letter: W
Question
Write a method named monthApart that accepts four integer parameters representing two calendar dates. Each date consists of a month (1 through 12) and a day (1 through the number of days in that month [28-31]). The method returns whether the dates are at least a month apart. Assume that all dates in this problem occur during the same year. For example, the following dates are all considered to be at least a month apart from 9/19 (September 19): 2/14, 7/25, 8/2, 8/19, 10/19, 10/20, and 11/5. The following dates are NOT at least a month apart from 9/19: 9/20, 9/28, 10/1, 10/15, and 10/18. Note that the first date could come before or after (or be the same as) the second date. Assume that all parameter values passed are valid.Sample calls:
Call | Returns | Because
monthApart( 6, 14, 9, 21) | true | June 14th is at least a month before September 21st
monthApart( 4, 5, 5, 15) | true | April 5th is at least a month before May 15th
monthApart( 4, 15, 5, 15) | true | April 15th is at least a month before May 15th
monthApart( 4, 16, 5, 15) |false | April 16th is NOT at least a month before May 15th
monthApart( 6, 14, 6, 8) | false | June 14th is NOT at least a month apart from June 8th
monthApart( 7, 7, 6, 8) | false | July 7th is NOT at least a month apart from June 8th
monthApart( 7, 8, 6, 8) | true | July 8th is at least a month after June 8th
monthApart( 10, 14, 7, 15) | true | October 14th is at least a month after July 15th
Explanation / Answer
import java.util.Scanner; public class DateDifference { public static int[] dateArray = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Please first date's month"); int m1 = input.nextInt(); System.out.println("Please first date's day"); int d1 = input.nextInt(); System.out.println("Please second date's month"); int m2 = input.nextInt(); System.out.println("Please second date's day"); int d2 = input.nextInt(); boolean isMonthApart = monthapart(m1, d1, m2, d2); if (isMonthApart) { System.out.println("Dates are atleast a month apart"); } else { System.out.println("Dates are not month apart"); } } public static boolean monthapart(int m1, int d1, int m2, int d2) { // Same month so they are not month apart if (m1 == m2) { return false; } // Difference of month is greater than 1 so date is atleast a month // apart if (m1 - m2 > 1) { return true; } int daysDifference = 0; // If month 1 is before month 2. if (m2 > m1) { // Get max days in month 1 int maxDaysMonth1 = dateArray[m1 - 1]; int numberOfDaysMonth1 = maxDaysMonth1 - d1; daysDifference = numberOfDaysMonth1 + d2; // Days Difference is greater than maximum number of days in month 1 // then return true if (daysDifference > maxDaysMonth1) { return true; } } else { // Month 2 is before month 1 // Get max days in month 2 int maxDaysMonth2 = dateArray[m2 - 1]; int numberOfDaysMonth2 = maxDaysMonth2 - d2; daysDifference = numberOfDaysMonth2 + d1; // Days Difference is greater than maximum number of days in month 2 // then return true if (daysDifference > maxDaysMonth2) { return true; } } return false; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.