0

I want to fix the date to month/day/year in my class (Gregorian Calendar) so it works with my tester program. I get errors when running my tester program, I think it has to do with my string toString() method but I have tried to fix it but keep getting errors. I do not understand how having my string output to month + day + year would not work correctly in outputting mmmm/dd/yyyy. Thank you for your help.

Errors:

Exception in thread "main" java.lang.IllegalArgumentException
at Date.<init>(Date.java:15)
at Assign8B.main(Assign8B.java:14)

Class

public class Date {
    private int day, month, year;
    public Date() {
        this.day = 1;
        this.month = 1;
        this.year = 1970;
    }
    public Date(int year, int month, int day) {
        if (year < 1582) {
            throw new IllegalArgumentException();
        } else if (month <= 0 && month > 12) {
            throw new IllegalArgumentException();
        } else if (!isLeapYear(year) && (month == 2 && day == 29)) {
            throw new IllegalArgumentException();
        } else {
            this.day = day;
            this.month = month;
            this.year = year;
        }
    }
    public void addDays(int days) {
        int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int step = 1;
   
        if(days < 0)
            step = -1;
  
        if(isLeapYear(year))
            daysOfMonth[1] = 29;

        int d = 0;
        while(d < days){
            d++;
            day += step;
            if(day > daysOfMonth[month-1]){
                day = 1;
                month++;
                if(month > 12){
                    year++;
                    month = 1;
                    if(isLeapYear(year))
                        daysOfMonth[1] = 29;
                    else
                        daysOfMonth[1] = 28;
                }
            }
            else if(day < 1) {
                month--;
                if(month == 0) {
                    month = 12;
                    year--;
                    if(isLeapYear(year))
                        daysOfMonth[1] = 29;
                    else
                        daysOfMonth[1] = 28;
                }
                day = daysOfMonth[month-1];
            }
        }
    }
    public void addWeeks(int weeks) {
        addDays(weeks * 7);
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
        this.day = day;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public boolean isLeapYear() {
        return isLeapYear(this.year);
    }
    public boolean isLeapYear(int year) {
        return(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
    }
    public int daysTo(Date other) {
        int days = 0;
        int d1, m1, y1, d2, m2, y2;
        int sign = 1;
   
        if(this.toString().compareTo(other.toString()) > 0){
            d1 = other.day;
            m1 = other.month;
            y1 = other.year;

            d2 = day;
            m2 = month;
            y2 = year;
            sign = -1;
        } else {
            d1 = day;
            m1 = month;
            y1 = year;

            d2 = other.day;
            m2 = other.month;
            y2 = other.year;
        }

        int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(isLeapYear(y1))
            daysOfMonth[1] = 29;

        while(d1 != d2 || m1 != m2 || y1 != y2){
            days++;
            d1++;
            if(d1 > daysOfMonth[m1-1]){
                d1 = 1;
                m1++;
                if(m1 > 12){
                    y1++;
                    m1 = 1;
                    if(isLeapYear(y1))
                        daysOfMonth[1] = 29;
                    else
                        daysOfMonth[1] = 28;
                }
            }
        }

        days = days * sign;
        return days;
    }

    public String longDate() {
        String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
        return months[month-1] + " " + day + ", " + year;
    }
    public String toString() {
        String s = month + "/" + day + "/" + year;
        return s;
    }
    public static int daysTo(Date one, Date two) {
        return one.daysTo(two);
    }

}

Tester Program

public class Assign8B {
// Part of the main method I'll use to test your class
// NO imports allowed from the JAVA API
public static void main(String[] a) {
    Date one = new Date(10,15,1582);    // start of Gregorian
    Date two = new Date(1,28,2020); // 2020 is a leap year
    
    one.addDays(1);     // advance one day (negative subtracts days)
    one.addWeeks(10);   // advance one week (negative allowed, yes)
    System.out.println(two.daysTo(one)); // -159645 days (negative)
    System.out.println(one.getDay());   // day is now the 25th (advanced)
    System.out.println(one.getMonth()); // returns 12, January is 1
    System.out.println(one.getYear());  // still 1582 from start
    System.out.println(one.isLeapYear());   // false for 1582
    System.out.println(one.toString()); // style is 12/25/1582
    
    try {
        Date three = new Date(12,33,1956); // obviously illegal
        Date four = new Date(2,29,2013); // illegal leap year
        three.setDay(31);       // fixes that day of month, OK
        four.setMonth(3);       // fixes the month, year still wrong
        four.setYear(1929);     // fixes the year, code not reached
    } catch (IllegalArgumentException e) {
        System.out.println("Illegal day attempted");
    }
    
    // Use UNIX zero of 01/01/70 for default, and create "longDate" output
    // I thought a long date was dinner with a person you don't like?
    Date five = new Date();
    System.out.println(five.longDate());  // January 1, 1970
    
    // Finally, let's understand what static methods are most commonly used for:
    System.out.println(Date.daysTo(one, two));  // still 159645 days (positive)
}

}

Rendless
  • 1
  • 1

1 Answers1

1

As @madprogrammer mentioned in the comments, you have to change your Date call because of the order of year month day in your own class constructor, and then look at your add days function and change how you add years in your code.

Zoe
  • 27,060
  • 21
  • 118
  • 148