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

Write a class called Date that includes three fields year, month and day. This c

ID: 3671664 • Letter: W

Question

Write a class called Date that includes three fields year, month and day. This class stores information about a single date (year, month and day). Your class should have constructor(s), accessors and mutators and it should implement the Comparable interface. Years take precedence over months, which take precedence over days. For example, Feb 19, 2016 comes after Nov 20, 2015.

The following class DateTest can be used to test the Date class that you wrote. It creates a list of the birthdays of the first 5 U.S. presidents in random order and puts them into sorted order. (Note: you can use Collections.sort() to sort your ArrayList after you implement the compareTo() method).

import java.util.*;

public class DateTest {
public static void main(String[] args) {

ArrayList<Date> dates = new ArrayList<Date>(); dates.add(newDate(4,13,1743)); //Jefferson dates.add(newDate(2,22,1732)); //Washington dates.add(newDate(3,16,1751)); //Madison dates.add(new Date(10, 30, 1735)); // Adams dates.add(newDate(4,28,1758)); //Monroe

System.out.println("birthdays = " + dates); Collections.sort(dates); System.out.println("birthdays = " + dates);

} }

When you execute the following code it should print:
birthdays = [4/13/1743, 2/22/1732, 3/16/1751, 10/30/1735, 4/28/1758] birthdays = [2/22/1732, 10/30/1735, 4/13/1743, 3/16/1751, 4/28/1758]

Explanation / Answer


CalendarDateTest.java


import java.util.*;

public class CalendarDateTest {
    public static void main(String[] args) {
        ArrayList<CalendarDate> dates = new ArrayList<CalendarDate>();
        dates.add(new CalendarDate(2, 22));   // Washington
        dates.add(new CalendarDate(10, 30)); // Adams
        dates.add(new CalendarDate(4, 13));   // Jefferson
        dates.add(new CalendarDate(3, 16));   // Madison
        dates.add(new CalendarDate(4, 28));   // Monroe

        System.out.println("birthdays = " + dates);
        Collections.sort(dates);
        System.out.println("birthdays = " + dates);
    }
}

CalendarDate.java
// The CalendarDate class stores information about a single calendar date
// (month and day but no year).

public class CalendarDate implements Comparable<CalendarDate> {
    private int month;
    private int day;

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

    // Compares this calendar date to another date.
    // Dates are compared by month and then by day.
    public int compareTo(CalendarDate other) {
        if (this.month != other.month) {
            return this.month - other.month;
        } else {
            return this.day - other.day;
        }
    }

    public int getMonth() {
        return this.month;
    }

    public int getDay() {
        return this.day;
    }

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

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