使用面向对象编程检查日期

时间:2014-11-30 17:33:52

标签: java date

这是我实验室要求我做的事情。

根据以下要求在Date.java文件中编写类Date:

1)实例变量: 月,日,年 - 所有都是type,int,都是私有的

2)构造者: 只有一个接受三个整数:月,日和年(按此顺序 - 如果三个参数不形成一致,则抛出异常IllegalArgumentException 日期对象,即无效日期。无效日期可能是: •月份,日期或年份小于1 •一个月大于12 •第1,3,5,7,8,10,12个月的日期大于31 •第4,6,9,11个月的日期超过30天 •在非闰年中,2月份的日期超过28天 •闰年2月份的日期超过29天 注意:如果它可以被4整除而不是100,那么一年是飞跃,除非它也可以被400整除 例如,1900年没有飞跃,但2000年是飞跃。

3)全套访问者和变异者: •int getMonth(),int getDay,int getYear() •void setMonth(int m),void setDay(int d),void setYear(int y) - 如果mutator会使Date对象不一致(无效),

应抛出异常IllegalArgumentException

4)方法toString,返回一个显示表单中日期的字符串:MMM-DD-YYYY 示例:JAN-07-2011 NOV-26-2014 FEB-29-2012 JUN-04-1992

5)未指定,但需要:您可能需要一些私有方法来帮助验证部分内容 日期和确定一年是否飞跃。

这是实验室13的代码。

public class Lab13 {
public static void main(String[] args) {
   Date d1 =  new Date( 11, 24, 2013 );
   String dateStr = "" + d1;
   if (!dateStr.equals("NOV-24-2013"))
      System.out.println( "ERROR: toString not returning NOV-24-2013 but instead: " + dateStr );

   if ( d1.getMonth() != Date.NOV )
      System.out.println( "ERROR: getMonth not returning NOV but instead: " + d1.getMonth() );

   if ( d1.getDay() != 24 )
      System.out.println( "ERROR: getDay not returning 24 but instead: " + d1.getDay() );

   if ( d1.getYear() != 2013 )
      System.out.println( "ERROR: getYear not returning 2013 but instead: " + d1.getYear() );

   d1.setMonth(12);
   d1.setDay(25);
   d1.setYear(2014);
   dateStr = ""+d1;
   if (!dateStr.equals("DEC-25-2014"))
      System.out.println( "ERROR: mutators(or toString) not returning DEC-25-2014 but instead: " + dateStr );

   try {       
      d1 = new Date( 2, 29, 2016 );
   } catch( IllegalArgumentException iae ) {
      System.out.println( "ERROR: Could not set Date to 2/29/2016" );
   }

   try {       
      d1 = new Date( 2, 29, 2000 );
   } catch( IllegalArgumentException iae ) {
      System.out.println( "ERROR: Could not set Date to 2/29/2000" );
   }

   // trying errors:
   Date d2 = null;

   try {
      d2 = new Date( 13, 14, 2015 );
      System.out.println( "ERROR: SHOULD HAVE thrown exception for month = 13" );
   } catch( IllegalArgumentException iae ) {}

   try {
      d2 = new Date( 0, 14, 2015 );
      System.out.println( "ERROR: SHOULD HAVE thrown exception for month = 0" );
   } catch( IllegalArgumentException iae ) {}

   try {
      d2 = new Date( 1, 32, 2015 );
      System.out.println( "ERROR: SHOULD HAVE thrown exception for day = 32 in JAN" );
   } catch( IllegalArgumentException iae ) {}

   try {
      d2 = new Date( 2, 30, 2016 );
      System.out.println( "ERROR: SHOULD HAVE thrown exception for day = 30 in Leap FEB" );
   } catch( IllegalArgumentException iae ) {}

   try {
      d2 = new Date( 2, 29, 1900 );
      System.out.println( "ERROR: SHOULD HAVE thrown exception for day = 29 in non-Leap FEB" );
   } catch( IllegalArgumentException iae ) {}

   try {
      d2 = new Date( 2, 29, 2014 );
      System.out.println( "ERROR: SHOULD HAVE thrown exception for day = 29 in non-Leap FEB" );
   } catch( IllegalArgumentException iae ) {}

   try {
      d2 = new Date( 4, 31, 2015 );
      System.out.println( "ERROR: SHOULD HAVE thrown exception for day = 31 in APR" );
   } catch( IllegalArgumentException iae ) {}

   try {
      d2 = new Date( 8, 0, 2012 );
      System.out.println( "ERROR: SHOULD HAVE thrown exception for day = 0" );
   } catch( IllegalArgumentException iae ) {}

   try {
      d2 = new Date( 8, 15, 0 );
      System.out.println( "ERROR: SHOULD HAVE thrown exception for year = 0" );
   } catch( IllegalArgumentException iae ) {}

   System.out.println("No output above means GREAT Date class!!");
}

}

这是我到目前为止为Date.java编写的代码

public class Date {
private int month;
private int day;
private int year;
public static void check (){
  int m = getMonth(m);
  int y = getYear(y);
  int d = getDay(d);
}
public static void getMonth(int m){
  if (m >= 1 && m <= 12) {
     m = month;
  } else {
    throw IllegalArgumentException("Date not between 1 and 12");
  }
}
public static void getYear(int y) {
  if (y >= 1) {
     y = year;
  } else { 
     throw IllegalArgumentException("Year can not be less than 1");
  }
}
public static void getDay (int d) {
  if (d >= 1 && day <= 31 ) {
     d = day;
  }else if (d < 30 && month == 4|| month == 6 || month == 9 || month == 11) {
     throw IllegalArgumentException("day can not be greater than 30");
  }else if (d < 28 && month == 2 && (year % 4 == 0) && (year % 100 == 0) && (year % 400 != 0) ||
        (year % 4 != 0) && (year % 100 != 0) && (year % 400 != 0)) {
     throw IllegalArgumentException("February can not have over 28 days in non leap year");
  }else if ( d < 29 && month == 2 && (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
     throw IllegalArgumentException("February can not have over 29 days in a leap year");
  }
}
} 

我遇到的问题是我的实例变量,并试图让他们接受实验室13课程中的数字。当然,如果有人能指出我做错的其他事情,特别是如何找到正确的日子,这将是伟大的。我经常向老师询问这些问题,而且大部分时间他都会回到我这里,但已经过了3天,我真的需要一些帮助。

0 个答案:

没有答案