-2

我有一个名为 Date 的课程:

public class Date {

  // fields

  private int month;
  private int day;
  private int year;

  // constructor

  public Date(int month, int day, int year) {
    this.month = month;
    this.day = day;
    this.year = year;
  }

  //check for leap year or not
  public static boolean isLeap(int year) {
    if (year % 4 != 0) {
      return false;
    } else if (year % 400 == 0) {
      return true;
    } else if (year % 100 == 0) {
      return false;
    } else {
      return true;
    }

  }

  public static boolean isValid(int month, int day, int year) {
    if (year < 1900) {
      return false;
    } else {
      if (month > 0 && month < 13) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
          return day > 0 && day <= 31;

        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
          return day > 0 && day <= 30;
        } else if (month == 2) {
          if (isLeap(year)) {
            return day > 0 && day <= 29;
          } else {
            return day > 0 && day <= 28;
          }
        }

      } else {
        return false;
      }
    }
    return true;
  }

  public int getMonth() {
    return month
  }

  public void setMonth(int month) {
    this.month = month;
  }

  public int getDay() {
    return day;
  }

  public void setDay(int day) {
    this.day = day;
  }

  public int getYear() {
    return year;
  }

  public void setYear(int year) {
    this.year = year;
  }

  public void setDate(int month, int day, int year) {
    if (isValid(month, day, year)) {
      setDay(day);
      setMonth(month);
      setYear(year);
    } else {
      setDay(1);
      setMonth(1);
      setYear(1900);
    }
  }
//

  public String displayDate() {
    return month + "/" + day + "/" + year;
  }

  public String add(int n) {
    if (n <= 20) {
      if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        if ((day + n > 0) && (day + n <= 31))
          day = day + n;
        else
          month += 1;
        if (month > 12)
          year += 1;
      }
      if (month == 4 || month == 6 || month == 9 || month == 11) {
        if ((day + n > 0) && (day + n <= 30))
          day = day + n;
        else
          month += 1;
        if (month > 12)
          year += 1;
      }
    }
    return month + "/" + day + "/" + year;
  }

  public String sub(int n) {
    if (n <= 20) {
      if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        if (day - n < 0)
          day = 31 + day - n;
        else
          month -= 1;
      if (month < 0)
        year -= 1;
      if (month == 4 || month == 6 || month == 9 || month == 11)
        if ((day - n < 0))
          day = 30 + day - n;
        else
          month -= 1;
      if (month < 0)
        year -= 1;

    }
    return month + "/" + day + "/" + year;
  }

}

而这个类来测试 Date 类:

public class MyDateTester {
    public static void main(String[] args) {
        new MyDateTester();
    }

    Date theClass;

    public MyDateTester(){
        theClass = new Date(6,1,1600);
        System.out.print("Month is: ");
        System.out.println(theClass.getMonth());
        System.out.print("Day is: ");
        System.out.println(theClass.getDay());
        System.out.print("Year is: ");
        System.out.println(theClass.getYear());
        System.out.println(theClass.displayDate());
        System.out.println(theClass.add(20));
        System.out.println(theClass.isValid(6,1,2000));
    }
}

当我给 Date 6、1、1600 值时,它仍然打印 6、1、1600,setDate 方法中的 isValid 方法没有正确设置日期。有任何想法吗?

4

2 回答 2

1

在您的测试中,您正在使用其构造函数创建一个日期。您的构造函数不调用setDate()isValid().

 public Date(int month, int day, int year){
    this.month = month;
    this.day = day;
    this.year = year;
 }

setDate()您可以通过调用Date 构造函数来最轻松地解决这个问题。

 public Date(int month, int day, int year){
    setDate( month, day, year );
 }
于 2013-05-26T15:54:43.083 回答
0

您必须在 Date 的构造函数中调用 setDate() 方法

于 2013-05-26T15:57:52.020 回答