Object-Oriented Programming with Java, part I – Exercise 92

This exercise takes the class MyDate and extends it to calculate the amount of years between two dates taking into account dates with less than year between them and dates where the compared date is after the initial.

My Code

Main

public class Main {
    public static void main(String[] args) {
        MyDate first = new MyDate(24, 12, 2009);
        MyDate second = new MyDate(1, 1, 2011);
        MyDate third = new MyDate(25, 12, 2010);

        System.out.println( first + " and " + second + " difference in years: " + second.differenceInYears(first) );
        System.out.println( second + " and " + first + " difference in years: " + first.differenceInYears(second) );
        System.out.println( first + " and " + third + " difference in years: " + third.differenceInYears(first) );
        System.out.println( third + " and " + first + " difference in years: " + first.differenceInYears(third) );
        System.out.println( third + " and " + second + " difference in years: " + second.differenceInYears(third) );
        System.out.println( second + " and " + third + " difference in years: " + third.differenceInYears(second) );
    }
}

MyDate

public class MyDate {
    private int day;
    private int month;
    private int year;

    public MyDate(int day, int montd, int year) {
        this.day = day;
        this.month = montd;
        this.year = year;
    }

    public String toString() {
        return this.day + "." + this.month + "." + this.year;
    }

    public boolean earlier(MyDate compared) {
        if (this.year < compared.year) {
            return true;
        }

        if (this.year == compared.year && this.month < compared.month) {
            return true;
        }

        if (this.year == compared.year && this.month == compared.month
                && this.day < compared.day) {
            return true;
        }

        return false;
    }
    
    public void advance(){
        this.day++;
        if (this.day == 31){
            this.day = 1;
            this.month++;
            if (this.month == 13){
                this.month = 1;
                this.year++;
            }
        }
    }
    
    public void advance(int numberOfDays){
        int i = 0;
        while (i < numberOfDays){
            this.advance();
            i++;
        }
    }
    
    public MyDate afterNumberOfDays(int days){
    MyDate newMyDate = new MyDate(this.day, this.month, this.year);

    // some code here
    newMyDate.advance(days);

    return newMyDate;
}
    
    public int differenceInYears(MyDate comparedDate){
        int days1 = this.day;
        days1 += this.month * 30;
        days1 += this.year * 365;
        
        int days2 = comparedDate.day;
        days2 += comparedDate.month * 30;
        days2 += comparedDate.year * 365;
        
        return Math.abs((days1 - days2) / 365);
        
    }

}

Model Code

MyDate

public class MyDate {

    private int day;

    private int month;

    private int year;

 

    public MyDate(int day, int montd, int year) {

        this.day = day;

        this.month = montd;

        this.year = year;

    }

 

    public String toString() {

        return this.day + "." + this.month + "." + this.year;

    }

 

    public boolean earlier(MyDate compared) {

        if (this.year < compared.year) {

            return true;

        }

 

        if (this.year == compared.year && this.month < compared.month) {

            return true;

        }

 

        if (this.year == compared.year && this.month == compared.month

                && this.day < compared.day) {

            return true;

        }

 

        return false;

    }

 

    public int differenceInYears(MyDate compared) {

        if (this.earlier(compared)) {

            return differenceWhenThisEarlier(compared);

        }

        

        int minusOneYear = 0;

        if (this.month < compared.month) {

            minusOneYear = 1; 

        } else if (this.month == compared.month && this.day < compared.day) {

            minusOneYear = 1; 

        }

 

        return this.year - compared.year - minusOneYear;

    }

 

    private int differenceWhenThisEarlier(MyDate compared) {

        int minusOneYear = 0;

 

        if (compared.month < this.month) {

            minusOneYear = 1;

        } else if (compared.month == this.month && compared.day < this.day) {

            minusOneYear = 1;

        }

 

        return compared.year - this.year - minusOneYear;

    }    

   

}

 

Comments

This is a rare case where my code ended up being so massively better than theirs that it should be the model code. It almost makes me think there must be something wrong with it.

Leave a comment