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

This exercise asks you to extend the MyDate class by advancing it one day, many days, and creating a new MyDate class.

My Code

Main

public class Main {

public static void main(String[] args) {
    MyDate day = new MyDate(25, 2, 2011);
    MyDate newDate = day.afterNumberOfDays(7);
    for (int i = 1; i <= 7; ++i) {
        System.out.println("Friday after  " + i + " weeks is " + newDate);
        newDate = newDate.afterNumberOfDays(7);
    }
    System.out.println("This week's Friday is " + day);
    System.out.println("The date 790 days from this week's Friday is  " + day.afterNumberOfDays(790));
}
}

MyDate


public class MyDate {

    private int day;
    private int month;
    private int year;

    public MyDate(int day, int month, int year) {
        this.day = day;
        this.month = month;
        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;
}

}

Model Code

Main

public class Main {

public static void main(String[] args) {
    MyDate day = new MyDate(25, 2, 2011);
    MyDate newDate = day.afterNumberOfDays(7);
    for (int i = 1; i <= 7; ++i) {
        System.out.println("Friday after  " + i + " weeks is " + newDate);
        newDate = newDate.afterNumberOfDays(7);
    }
    System.out.println("This week's Friday is " + day);
    System.out.println("The date 790 days from this week's Friday is  " + day.afterNumberOfDays(790));
}
}

MyDate

public class MyDate {

 

    private int day;

    private int month;

    private int year;

 

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

        this.day = day;

        this.month = month;

        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 = this.day + 1;

 

        if (this.day > 30) {

            this.month = this.month + 1;

            this.day = 1;

 

            if (this.month > 12) {

                this.year = this.year + 1;

                this.month = 1;

            }

        }

    }

 

    public void advance(int numberOfDays) {

        int laskuri = 0;

        while (laskuri < numberOfDays) {

            this.advance();

            laskuri = laskuri + 1;

        }

    }

 

    public MyDate afterNumberOfDays(int days) {

        MyDate date = new MyDate(this.day, this.month, this.year);

 

        int counter = 0;

        while (counter < days) {

            date.advance();

            counter = counter + 1;

        }

 

        return date;

    }

}

Comments

I think that all worked out better than expected.

Leave a comment