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

here\'s the requirements The Date class The date class represents a date - month

ID: 3639645 • Letter: H

Question

here's the requirements


The Date class

The date class represents a date - month/day/year. It implements the interfaces Comparable and Cloneable.
Most of the class has been provided to you. Download the Date.java file.

You are required to implement the compareTo(Date date) and clone() methods.

Parts of the code which you are required to fill are marked by "YOUR CODE GOES HERE".
Properties:
----------
1) private int day.
2) private int month.
4) private int year.

Constructors:
------------
1) public Date(int month, int day, int year)
// Set's the properties appropriately

Methods:
--------
-get/set methods for all the properties (already implemented):

-public int compareTo(Date date):
Returns 1 if this date is smaller than the date passed in. (For ex. Date(9,1,2011) < Date(9,1,2012) = return 1)
Returns -1 if this date is larger than the date passed in. (For ex. Date(9,1,2011) > Date(9,1,2010) = return -1)
Returns 0 if the dates are equal

-public Object clone() throws CloneNotSupportedException:
Returns a deep copy

-toString() - The toString() method is public and returns a String. (already implemented)
It should return a String in this format:
month/day/year

Explanation / Answer


public class Date implements Comparable<Date> {

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

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

    public static void main(String[] args) {
        Date date1 = new Date(9,1,2011);
        Date date2 = new Date(9,1,2010);
        System.out.println(date1.compareTo(date2));
    }
    
    public int compareTo(Date date) {
        Date local = null;
        try {
            local = (Date) clone();
        } catch (Exception e) {
        }

        if (date.year == local.year && date.month == local.month && date.day == local.day) {
            return 0;
        }
        
        if (local.year< date.year) {
            return 1;
        }
        if (local.year >date.year) {
            return -1;
        }
        if (local.day< date.day) {
            return 1;
        }
        if (local.day >date.day) {
            return -1;
        }        
        return -2;
    }

    public Object clone() throws CloneNotSupportedException {
        Date obj = new Date(this.month, this.day, this.year);
        return obj;
    }

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

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
} Java2html
public class Date implements Comparable<Date> {

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

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

    public static void main(String[] args) {
        Date date1 = new Date(9,1,2011);
        Date date2 = new Date(9,1,2010);
        System.out.println(date1.compareTo(date2));
    }
    
    public int compareTo(Date date) {
        Date local = null;
        try {
            local = (Date) clone();
        } catch (Exception e) {
        }

        if (date.year == local.year && date.month == local.month && date.day == local.day) {
            return 0;
        }
        
        if (local.year< date.year) {
            return 1;
        }
        if (local.year >date.year) {
            return -1;
        }
        if (local.day< date.day) {
            return 1;
        }
        if (local.day >date.day) {
            return -1;
        }        
        return -2;
    }

    public Object clone() throws CloneNotSupportedException {
        Date obj = new Date(this.month, this.day, this.year);
        return obj;
    }

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

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
} Java2html