Java 用面向对象编程检查日期
这是我的实验室要求我做的 根据以下要求在文件Date.java中编写类Date: 1) 实例变量: 月、日、年–都是type、int,都是private 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() •无效设置月(整数m)、无效设置日(整数d)、无效设置年(整数y) –如果变异体会使日期对象不一致(无效), 应引发异常IllegalArgumentException 4) 方法toString返回一个字符串,该字符串以以下形式显示日期:MMM-DD-YYYY 示例:2011年1月7日2014年11月26日2012年2月29日1992年6月4日 5) 未指定,但需要:您可能需要一些私有方法来帮助验证 日期和确定年份是否为闰年 这是实验室13的代码Java 用面向对象编程检查日期,java,date,Java,Date,这是我的实验室要求我做的 根据以下要求在文件Date.java中编写类Date: 1) 实例变量: 月、日、年–都是type、int,都是private 2) 建造商: 只有一个接受三个整数:月、日和年(按该顺序) --如果这三个参数不一致,则抛出异常IllegalArgumentException 日期对象,即无效日期。无效日期可能: •具有小于1的月、日或年 •一个月大于12个月 •第1、3、5、7、8、10、12个月的天数大于31天 •第4、6、9、11个月的天数大于30天 •在非闰年中,
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");
}
}
}
公开课日期{
私人整数月;
私人国际日;
私人国际年;
公共静态无效检查(){
int m=每月(m);
int y=getYear(y);
int d=getDay(d);
}
公共静态无效月(整数m){
如果(m>=1&&m=1){
y=年;
}否则{
抛出IllegalArgumentException(“年份不能小于1”);
}
}
公共静态void getDay(int d){
如果(d>=1&&day,我首先在Date类中创建一个构造函数,满足Lab13中的这一行:Date d1=new Date(11,24,2013)
。就像我将Date d1=new Date();放入我的Date类no中一样。这是在创建Date类的对象。您需要一个类似于Date(int month,int day,int year)的构造函数
在您的Date类中。这可能会有帮助:哦,好吧,这更像是我将参数传递给一个方法。