public class DateDemo { public static void main(String[] args) { System.out.println("number of months in a year == " + MyDate.numberOfMonths()); System.out.println(); MyDate d = new MyDate(1, 29, 2020); System.out.println(d); //means System.out.println(d.toString()); d.next(); System.out.println(d); d.next(10); System.out.println(d); System.exit(0); } } class MyDate { private static final int[] length = { 0, //so January will have subscript 1 31, //January 28, //February: ignoring leap years for the time being 31, //March 30, //April 31, //May 30, //June 31, //July 31, //August 30, //September 31, //October 30, //November 31 //December }; private int year; private int month; //1 to 12 inclusive private int day; public MyDate(int month, int day, int year) { //constructor if (year == 0) { System.err.println("There was no Year Zero."); System.exit(1); //should've thrown an exception } this.year = year; if (month < 1 || month > 12) { //or month >= length.length System.err.println("bad month " + month); System.exit(1); } this.month = month; if (day < 1 || day > length[month]) { System.err.println("bad day " + day); System.exit(1); } this.day = day; } public String toString() { return month + "/" + day + "/" + year; //Could also say //return this.month + "/" + this.day + "/" + this.year; } public void next() { if (++day > length[month]) { day = 1; if (++month > 12) { month = 1; if (year == Integer.MAX_VALUE) { System.err.println("already in year " + year); System.exit(1); } ++year; } } } public void next(int i) { for (; i > 0; --i) { next(); //One method can call another method. } } //Return the Julian date of this date (1 to 365 inclusive). //For example, the Julian date of April 16 is 31 + 28 + 31 + 16 == 106. public int julian() { //You will write the body of this method //in place of the following dummy statement. return 0; } /* Like the compareTo method in BubbleSortStrings.java. Return 0 if this MyDate is the same date as the other. Return a negative int if this MyDate is earlier than the other. Return a positive int if this MyDate is later than the other. */ public int compareTo(MyDate other) { /* You will write the body of this method in place of the following dummy statement. The fields of this date are year, month, day. For clarity, you can also call then this.year, this.month, this.day The fields of the other date are other.year, other.month, other.day */ return 0; } public static int numberOfMonths() { return length.length - 1; } public static int numberOfDays() { /* You will write the body of this method in place of the following dummy statement. It should return 365, the number of days in a year. */ return 0; } }