Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Java class called Date that includes three fields year, month and day. This class stores information about a single date (year, month and

Write a Java 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, 2019 comes after Nov 20, 2018.

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 dates = new ArrayList();

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]

Step by Step Solution

3.46 Rating (156 Votes )

There are 3 Steps involved in it

Step: 1

DateTestjava import javautil public class DateTest public static void mainString args ArrayList d... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Elementary Statistics

Authors: Robert R. Johnson, Patricia J. Kuby

11th Edition

978-053873350, 9781133169321, 538733500, 1133169325, 978-0538733502

More Books

Students also viewed these Accounting questions

Question

Describe disk attachment technologies and common implementations

Answered: 1 week ago

Question

How can NAFTA be beneficial to suppliers of Walmart?

Answered: 1 week ago