CS235 First Help Session Implement a simple Date class. You should be able to represent any date from January 1, 1800, to December 31, 2500; subtract two dates; increment a date by a number of days; and compare two dates using both equals and compareTo. A Date is represented internally as the number of days since some starting time which, here, is the start of 1800. This makes all methods except for construction and toString trivial. The rule for leap years is a year is a leap year if it is divisible by 4 and not divisible by 100 unless it is also divisible by 400. Thus 1800, 1900, and 2100 are not leap years, but 2000 is. Once you have decided on the specifications, you can do an implementation. The difficult part is converting between the internal and external representations of a date. What follows is a possible algorithm. Set up two arrays that are static fields. The first array, daysTillFirstOfMonth, will contain the number of days until the first of each month in a nonleap year. This it contains 0, 31, 59, 90, and so on. The second array, daysTillJan1, will contain the number of days until the first of each year, starting with firstYear. Thus it contains 0, 365, 730, 1095, 1460, 1826, and so on because 1800 is not a leap year, but 1804 is. You should have your program initialize this array once using a static initializer. You can then use the array to convert from the internal representation to the external representation. The steps to complete this project will be divided into three parts. Part A will contain mostly steps that will review basic Java functions and package creation. Part B will contain most of the the algorithm for the project. Part C will contain some Java I/O practice. The help sessions will focus mainly on Part A and C. Part A Class 1 Date: 1. Create a directory structure cs235/time. 2. Create a Date.java file, open it with a text editor. 3. Put Date.java into a package following the directory structure. 4. Create the Date class and add a count(int) variable, also create a getter method and setter method for count. (A method to return the count and a method to set the count to another int.) 5. Create a default constructor (also known as a no-argument constructor) that defaults to setting the internal count to 1 (thus making it January 1, 1800). 6a. Create a main method to test our Date class. 6b. Create a date with the Default constructor. Print out its count to the screen. Check it. (Its value should be 1.) 6c. Set the dates count to 5000. Print out the count and check it. 7. Statically set up an array for the daysTillFirstOfMonth. (These values will be: 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334) 8. Create a method that the default constructor calls to setup the other conversion array daysTillJan1. (Remember to take into account leap years.) Print out the first 20 values you compute and compare them to values below. 0,365,730,1095,1460,1826,2191,2556,2921,3287,3652,4017,4382,4748,5113,5478, 5843,6209,6574,6939 Part B 9. From now on have all constructors call the method in step 8 (to setup the conversion array daysTillJan1). 10a. Create a copy constructor. 10b. Test your copy constructor by creating a copy of the date from step 6 and printing its count to the screen. 11. Create a constructor that takes a month(int), day(int), year(int). 12a. Create a method that the constructor of step 11 calls to set the count variable based on the month, day, and year. (Remember to take into account leap years.) 12b. Test the constructor that takes a month(int), day(int), year(int) by giving it the date 1,2,1800 and see if it prints out a count of 2. 13a. Import the StringTokenizer or Split. Create a constructor that takes a string in the form of "month day year" (Ex. 12 25 2005), use a StringTokenizer or Split to parse out the month, day, and year, and then call the method described in step 12. 13b. Test the constructor that takes a string by giving it the value "1 3 1800" and see if it prints out a count of 3. 14. Create a toString method that takes the Date object's count and turns it into a string of form "month day year" (Ex. 9 15 1923). (Remember to take into account leap years.) You will use this method to print your dates to the screen. 15. Create a date with the Default constructor. Print it to the screen. Check it. 16. Create a date using the string constructor for January 5th. Print it to the screen and check it. 17. Create a date using the string constructor for December 25th. Print it to the screen and check it. 18. Create a date using the constructor that takes a month, day, year for January 9th, 1800. Print it to the screen and check it. 19. Create a copy of January 9th with the copy constructor. 20. Create an equals method that takes an Object as a parameter. Use instanceof to check if the object is a Date. Cast the object to a Date object, compare the counts, if they are equal return true, otherwise return false. 21. Create a compareTo method that takes a date(date2) as parameter. Return -1 if this date comes before date2, return 0 if this date is the same as date2, and return 1 if this date comes after date2. 22. Back in our main method to test Date class, use the equals method to check if January 9th, 1800 equals its copy. Print the result to the screen and check it. 23a. Use the compareTo method to check if Jan9th equals its copy. Print the result to the screen and check it. 23b. Use the equals and compareTo method to check how January 9th compare with January 5th. Print the results to the screen and check them. 24. Create a method to increment a date by a number of days(int). 25. Create a method to subtract two dates that takes a date(date2) as a parameter. This method subtracts date2's count from this date and returns the difference of days between the dates. 26. Back in our main method to test Date class, increment the copy's date by 5 days. Print the new date to the screen. Use the equals method and compareTo on Jan9th to compare it and the copy. Print and check the results. 27. Subtract Jan9th from the copy. Print the difference of days to the screen. 28. Test your methods with a few more dates at least including Feb. 29, 1804, Mar. 1, 1804, and any others you would like to try out. Part C Class 2 DateIO 1. Create another java file/class for I/O in the same folder. 2. Put DateIO.java into a package following the file structure. 3. Create the DateIO class. 4. Import FileInputStream, Scanner, PrintWriter and ArrayList. 5a. Create a static method to read dates from a file specified by filename and returns the dates in an ArrayList. (Ex. public static ArrayList readDates(String filename)) Dates are situated as "month day year" one date per line. (Ex. 12 3 2003 1 4 1999 ) 5b. Create an empty ArrayList. 5c. Create a try/catch statement. Have the catch catch all exceptions and print out an error message to the screen. 5d. Use a Scanner to read in the month, day, and year for each date, create a date, and add it to the ArrayList. 5e. Return the ArrayList. 6a. Create a static method to write an ArrayList of dates to a file specifed by filename(String). (Ex. public static void writeDateFile(String filename, ArrayList list)) 6b. Create a try/catch statement. Have the catch catch all exceptions and print out an error message to the screen. 6c. Use a PrintWriter to write out the ArrayList of dates to the file specified by filename. 7. Import ArrayList into Date.java. 8. Return back to our main method in Date.java to test our new methods. 9. Assign an ArrayList to be the return from a static call to our readMethod on dates.txt. (Ex. ArrayList al = DateRW.readDates("dates.txt")) 10. For each date in the ArrayList print it out to the screen. (Results should be: 11 1 2002 5 15 1968 8 30 1812 3 1 1808 2 29 2004 12 30 1999 3 1 1807 ) 11. Increment each date by 10 days. 12. Statically call our write method to write the new dates to a text file called datesPlus10.txt (Results should be: 11 11 2002 5 25 1968 9 9 1812 3 11 1808 3 10 2004 1 9 2000 3 11 1807 ) Congratulations, you are done.