SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Program Logic And Indefinite Loops
Exercise:
Exercises
Question:16 | ISBN:9780136091813 | Edition: 2

Question

Write a method named monthApart that accepts four integer parameters, m1, d1, m2, and d2, 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]). Assume that all parameter values passed are valid. The method should return true if the dates are at least a month apart and false otherwise. For example, the call of monthApart(4, 15, 5, 22) would return true while the call of monthApart(9, 19, 10, 17) would return false. Assume that all the dates in this problem
occur during the same year. Note that the first date could come before or after the second date.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

// package indefinite_loops;

public class MonthApart {

    public static boolean monthApart(int m1, int d1, int m2, int d2) {
        int daysOfMonth = -1;
        int monthMin = Math.min(m1, m2);

        // below swith takes the number of days in that month
        switch (monthMin) {
        case 1:
            daysOfMonth = 31;
            break;
        case 2:
            daysOfMonth = 28;
            break;
        case 3:
            daysOfMonth = 31;
            break;
        case 4:
            daysOfMonth = 30;
            break;
        case 5:
            daysOfMonth = 31;
            break;
        case 6:
            daysOfMonth = 30;
            break;
        case 7:
            daysOfMonth = 31;
            break;
        case 8:
            daysOfMonth = 31;
            break;
        case 9:
            daysOfMonth = 30;
            break;
        case 10:
            daysOfMonth = 31;
            break;
        case 11:
            daysOfMonth = 30;
            break;
        case 12:
            daysOfMonth = 31;
            break;
        }

        int dayFirst = -1;
        int daySecond = -1;
        if (m1 < m2) {
            dayFirst = d1;
            daySecond = d2;
        } else if (m1 > m2) {
            dayFirst = d2;
            daySecond = d1;
        }

        // return false if it is same month
        if (m1 == m2) { // Same month
            return false;
        }
        // return true if more than month apart
        else if (Math.abs(m1 - m2) > 1) {
            return true;

        } else if (Math.abs(m1 - m2) == 1) {

            if (((daysOfMonth - dayFirst) + daySecond) < 30) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    
    
    public static void main(String args[]) {
        boolean isMonthApart = monthApart(4,15,5,15);
        System.out.println("does the above dates at least month apart: "+isMonthApart);
    }

}
Output:

does the above dates at least month apart: true

 

0 0

Discussions

Post the discussion to improve the above solution.