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

Write a class named, Date, to store a date: month, day and year. Each of these s

ID: 3906638 • Letter: W

Question

Write a class named, Date, to store a date: month, day and year. Each of these should be stored as an integer. The class should contain all of the following public constructors and methods. These methods should have the EXACT same signature (method name and formal parameter list) and return type as given below. Be careful NOT to change the anything about the signatures and return types. In other words: DO NOT change anything about the signatures and return types as given below! This Date class you will write will be used as a new data type to create Date objects in other clients. A client is simply an application that uses a particular class. The Date class should contain no input or output statements whatsoever once it is submitted for grading. A client of the Date class would be responsible for doing its own input and/or output. Your instructor has a client that uses the Date class and all of its methods. This client is used to test your submitted Date.java. You will want to write your own application (client) that uses your Date class so that you can test your implementation of the Date class described below. Store your Date class client as a separate Java application in the same folder as your Date.java. To test Date.java include in your client statements to create Date objects, print Date objects, and invoke all of the other methods you implement in your Date class. Do not submit this client as it is only for your own testing. Submit only Date.java. Your instructor will expect it to compile and be usable by his own client in all the ways (methods) described below.

There are only two parts to this coding assignment I really do not understand:

public void addDays(int amt):

Change the current date by adding the number of days in the formal parameter. The value of the parameter could be positive or negative: a negative value results in making the date an earlier date. Examples: Adding 30 days to JAN-01-2000 gives JAN-31-2000 Adding 366 days to JAN-01-2000 gives JAN-01-2001 Adding -366 days to JAN-01-2000 gives DEC-31-1998 Adding -1000 days to JAN-01-2000 gives APR-06-1997

public int getDaysApart( Date other ):

Returns the difference between two dates in number of days. The retuned value is always a positive value. Examples: Days between JAN-01-2000 and JAN-01-2000 = 0 Days between JAN-01-2000 and JAN-11-2000 = 10 Days between JAN-01-2000 and APR-10-2000 = 100 Days between JAN-01-2000 and JUL-19-2000 = 200 Days between JAN-01-2000 and DEC-31-2000 = 365 Days between JAN-01-2000 and SEP-27-2002 = 1000

Could anyone help with these two aspects?

***We are not allowed to use any Calendar or existing Date java functions in the answer***

Explanation / Answer

Hello, I have answered a similar question before, so I made some slight changes to my own solution to satisfy your requirements. The below code will work 100% as expected. If you have any doubts, drop a comment. Thanks.

/ / Date.java

public class Date {

                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 };

                // array to store month names

                private String[] months = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN",

                                                "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };

                /**

                * 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

                * and getDaysApart methods)

                */

                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

                * addDays)

                */

                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];

                                }

                }

                /**

                * method to add amt number of days to this date

                *

                * @param amt

                *            - number of days to be added

                */

                public void addDays(int amt) {

                                // calling nextDay method for amt number of times if amt is positive

                                if (amt > 0) {

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

                                                                nextDay();

                                                }

                                } else {

                                                // calling previousDay method for amt number of times if amt is

                                                // negative

                                                for (int i = 0; i < Math.abs(amt); i++) {

                                                                previousDay();

                                                }

                                }

                }

                /**

                * method to find the number of days in between two dates

                *

                * @param other

                *            - the other date

                */

                public int getDaysApart(Date other) {

                                int difference = 0;

                                Date d1 = null, d2 = null;

                                if (this.equals(other)) {

                                                // same date

                                                return 0;

                                }

                                // always making d1 the older date

                                else if (this.isLessThan(other)) {

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

                                                d2 = other;

                                } else {

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

                                                d1 = other;

                                }

                                //looping until d1 is equal to d2

                                while (!d1.equals(d2)) {

                                                d1.nextDay();//moving to the next day

                                                difference++;

                                }

                                return difference;

                }

               

                /**

                * 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 + "-" + months[month - 1] + "-" + year;

                }

                @Override

                public boolean equals(Object o) {

                                if (o instanceof Date) {

                                                Date d = (Date) o;

                                                if (d.day == this.day && d.month == this.month

                                                                                && d.year == this.year) {

                                                                //same date

                                                                return true;

                                                }

                                }

                                return false;

                }

                /**

                * a method to check if current date is smaller than a given date

                */

                public boolean isLessThan(Date other) {

                                //comparing year, month and days

                                if (this.year < other.year) {

                                                return true;

                                } else if (this.year == other.year) {

                                                if (this.month < other.month) {

                                                                return true;

                                                } else if (this.month == other.month) {

                                                                if (this.day < other.day) {

                                                                                return true;

                                                                }

                                                }

                                }

                                return false;

                }

               

}

// Client.java (tester class)

public class Client {

                public static void main(String[] args) {

                                /**

                                * thoroughly testing Date class and methods

                                */

                                Date date1=new Date(21, 6, 2018);

                                System.out.println("Date1: "+date1);

                                System.out.println("Adding 1000 days to date1");

                                date1.addDays(1000);

                                System.out.println("Date1: "+date1);

                                Date date2=new Date(27, 3, 2021);

                                System.out.println("Date2: "+date2);

                                System.out.println("Days between date1 and date2: "+date1.getDaysApart(date2));

                                System.out.println("Subtracting 1000 days from date1");

                                date1.addDays(-1000);

                                System.out.println("Date1: "+date1);

                }

}

/*OUTPUT*/

Date1: 21-JUN-2018

Adding 1000 days to date1

Date1: 17-MAR-2021

Date2: 27-MAR-2021

Days between date1 and date2: 10

Subtracting 1000 days from date1

Date1: 21-JUN-2018

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote