Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab Exercise 07.1 Date Interface This exercise will give you experience in defin

ID: 3905863 • Letter: L

Question

Lab Exercise 07.1

Date Interface

This exercise will give you experience in defining a class and implementing it and then adding an interface. The class that you will define will be named Date and will contain the day, month and year of a date. It will contain 2 constructors, (a) one to construct an object with no particular values for day, month and year and (b) a second that will accept 3 arguments (day, month and year) and construct an object containing these values.

The class will contain additional methods as follows:

getDay          returns the value of the day variable

getMonth     returns the value of the month variable

getYear         returns the value of the year variable

toString         returns a date string in standard format

The interface which your date class will inherit will be as follows:

public interface Elapsed

{

// a year can have 365 or 366 days,

//   depending on “leap-year”.

//   February can have 28 or 29 days,

//   also depending on “leap-year”.

public static final int DAYS_WEEK = 7;

public static final int WEEKS_YEAR = 52;

// add any number of days to a date

public abstract Date addDays(int d);

// subtract any number of days from a date

public abstract Date subDays(int d);

}

Testing:

Files:

     Main.java                 supplied by blackboard

Elapsed.java                   supplied by you

Date.java                        supplied by you


Main.java

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Date.java

public class Date implements Elapsed {

                private int day;

                private int month;

                private int year;

                // an array to store number of days in a month for all 12 months

                private int[] daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,

                                                31 };

                /**

                * constructor to initialize day, month, year values, assuming that all

                * values are valid

                */

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

                                // assuming day,month,year values are valid

                                this.year = year;

                                if (isLeapYear()) {

                                                // its a leap year, so updating the days in month for february

                                                daysInMonth[1] = 29;

                                }

                                this.day = day;

                                this.month = month;

                }

                /**

                * a private method to move into the next day (helper method for addDays)

                */

                private void nextDay() {

                                day++;// next day

                                if (day > daysInMonth[month - 1]) {

                                                // next month

                                                day = 1;

                                                month++;

                                }

                                if (month > 12) {

                                                // next year

                                                month = 1;

                                                year++;

                                }

                                // updating number of days in february as per the year

                                if (isLeapYear()) {

                                                daysInMonth[1] = 29;

                                } else {

                                                daysInMonth[1] = 28;

                                }

                }

                /**

                * a private method to move into the previous day (helper method for

                * subDays)

                */

                private void previousDay() {

                                day--;// previous day

                                if (day < 1) {

                                                // previous month

                                                month--;

                                                if (month < 1) {

                                                                month = 12;

                                                                // previous year

                                                                year--;

                                                }

                                                // updating days in month for february

                                                if (isLeapYear()) {

                                                                daysInMonth[1] = 29;

                                                } else {

                                                                daysInMonth[1] = 28;

                                                }

                                                // updating current day value

                                                day = daysInMonth[month - 1];

                                }

                }

                @Override

                public Date addDays(int delta) {

                                // defining a date object

                                Date date = new Date(day, month, year);

                                // calling nextDay method for delta number of times

                                for (int i = 0; i < delta; i++) {

                                                date.nextDay();

                                }

                                // returning new date

                                return date;

                }

                @Override

                public Date subDays(int delta) {

                                // defining a date object

                                Date date = new Date(day, month, year);

                                // calling previousDay method for delta number of times

                                for (int i = 0; i < delta; i++) {

                                                date.previousDay();

                                }

                                // returning new date

                                return date;

                }

                /**

                * a private helper method to check if the current year is leap year or not

                */

                private boolean isLeapYear() {

                                return ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0));

                }

                @Override

                public String toString() {

                                //returning the current date as a properly formatted String

                                return "Day: " + day + ", Month: " + month + ", Year: " + year;

                }

}

// Elapsed.java (no changes)

public interface Elapsed

{

                // a year can have 365 or 366 days,

                // depending on “leap-year”.

                // February can have 28 or 29 days,

                // also depending on “leap-year”.

                public static final int DAYS_WEEK = 7;

                public static final int WEEKS_YEAR = 52;

                // add any number of days to a date

                public abstract Date addDays(int d);

                // subtract any number of days from a date

                public abstract Date subDays(int d);

}

// Main.java (no changes)

public class Main {

                private static Date today;

                private static Date tomorrow;

                private static Date yesterday;

                public static void main(String[] args) {

                                runTest(15, 3, 44, 7);

                                runTest(21, 3, 2018, 365);

                                runTest(15, 4, 1865, 10000);

                                runTest(1, 1, 2000, 90);

                                runTest(1, 1, 1990, 3650);

                                runTest(1, 1, 1999, 3650);

                }

                private static void runTest(int day, int month, int year, int delta) {

                                today = new Date(day, month, year);

                                tomorrow = today.addDays(delta);

                                yesterday = tomorrow.subDays(delta);

                                System.out.println("Today's " + today.toString());

                                System.out.println("Adding " + delta + " days.");

                                System.out.println("The future " + tomorrow.toString());

                                System.out.println("Subtracting " + delta + " days.");

                                System.out.println("Back to today's " + yesterday.toString());

                                System.out.println();

                }

}

/*OUTPUT*/

Today's Day: 15, Month: 3, Year: 44

Adding 7 days.

The future Day: 22, Month: 3, Year: 44

Subtracting 7 days.

Back to today's Day: 15, Month: 3, Year: 44

Today's Day: 21, Month: 3, Year: 2018

Adding 365 days.

The future Day: 21, Month: 3, Year: 2019

Subtracting 365 days.

Back to today's Day: 21, Month: 3, Year: 2018

Today's Day: 15, Month: 4, Year: 1865

Adding 10000 days.

The future Day: 31, Month: 8, Year: 1892

Subtracting 10000 days.

Back to today's Day: 15, Month: 4, Year: 1865

Today's Day: 1, Month: 1, Year: 2000

Adding 90 days.

The future Day: 31, Month: 3, Year: 2000

Subtracting 90 days.

Back to today's Day: 1, Month: 1, Year: 2000

Today's Day: 1, Month: 1, Year: 1990

Adding 3650 days.

The future Day: 30, Month: 12, Year: 1999

Subtracting 3650 days.

Back to today's Day: 1, Month: 1, Year: 1990

Today's Day: 1, Month: 1, Year: 1999

Adding 3650 days.

The future Day: 29, Month: 12, Year: 2008

Subtracting 3650 days.

Back to today's Day: 1, Month: 1, Year: 1999