这就是我的实验室要求我做的事情 .
根据以下要求在Date.java文件中编写类Date:
1)实例变量:月,日,年 - 都是type,int,都是私有的
2)构造函数:只有一个接受三个整数:月,日和年(按顺序 - 抛出异常,IllegalArgumentException,如果三个参数不形成一致的日期对象,即无效的日期 . 无效的日期可能:•一个月,一天或一年小于1•一个月大于12•一天大于31个月1,3,5,7,8,10,12•有一天大于30个月,4个月,9个月,11个月•非闰年2月份超过28天•闰年2月份超过29天NB:如果可以被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天,我真的需要一些帮助 .