Question: Write a class named, Date , to store a date: month, day and year. Each of these should be stored as an integer . The

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.

Three instance variables ONLY (no more!): one for each part of the date: month, day and year

Use named constants to make your code easier to read and understand. For example, plan to use named constants for all month integer values, as in: public static final int JAN = 1; . . . and the other eleven months.

Default constructor (takes no parameters) should set current date to January 1, 2000

Constructor that takes three parameters: month, day and year: public Date( int month, int day, int year )

Copy constructor: this is a constructor that accepts one parameter of type Date and then sets the receiving (or executing) objects instance variables equal to those of the parameter object. The result is that the receiving object is a copy of the formal parameter object: public Date( Date d )

Accessors: one for each of the three instance variables: public int getMonth() public int getDay() public int getYear()

Mutators: one for each of the three instance variables: public void setMonth( int m ) public void setDay( int d ) public void setYear( int y ) ALSO REQUIRED: all mutators and constructors must validate values from client before setting the corresponding instance variables. When a client passes in invalid values, the application should be terminated by throwing an exception as follows: throw new IllegalArgumentException( ); The error message should contain as much helpful info as possible. For example: "Day value 30 is invalid for month value 2" or "Year must be positive ( > 0 )" or "Month must be between 1 and 12, inclusive"

public String toString() This method is a special method for converting a Data object into a String in the following date format: MMM-DD-YYYY Examples: JAN-01-2000 FEB-29-1996 JUL-04-2004 DEC-25-0800 All month names are three letters and all uppercase. All day values are two digits and all years are four digits. What makes toString special is that it gets called automatically. So in your tester code you will be able to test this method and view the contents of a Date object, d1, with the following print statement: System.out.println(My date is: + d1 ); The toString method does not print anything itself. All it does is return a String. To use the power of printf to help format your dates, use the format static method in the String class: it works just like printf, but returns the formatted string rather than print it. See this method's documentation in the Java API under the String class. Also see the example, FormatExample.java.

public boolean equals( Date other ) Return true if the receiving date and the formal parameter Date are equal, i.e., the month, day AND year are identical

public int getDayNumber() Return the day number of the current date within the current year. Example, Dec. 31, is either day number 366 (in a leap year) or day number 365.

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 compareTo( Date other ) Read about compareTo in the section about Strings, both in the text and in the Java API. This method compares two dates: the current and the parameter (other). The return value is one of three integers: -1 if current < other ==> current comes before other 1 if current > other ==> current comes after other 0 if the two are equal ==> dates are equal

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

You should need helper methods as you design and develop the required methods above. Create private helper methods to simplify complex tasks and to eliminate redundancy in your code, like having the same few lines of code repeated every time you need to do some task however simple. You must make these helper methods private. You may name them whatever you wish. Group all private methods together AFTER all the public methods.

IMPORT RESTRICTION: Java has at least one Date class and several other date- and calendar-related classes in its library. You may NOT use any of these library classes.

Useful: You will find it helpful to create a separate class in the same folder as Date.java to use as a tester application. This separate class could be named, DateTester.java. As you complete part of the required work for Date.java, add corresponding code to test the latest Date features to your tester application.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!