构造日期类MyDate类,包含年月日,提供相应的get和set函数,提供void print()函数打印日期,提供int compare(MyDate d)测试当前对象和参数对象d的早晚,如果早则返回-1,晚则返回1,相等则返回0 在main函数中,读入两个日期对象,输出第一个日期对象的信息,输出两个对象的比较结果
输入格式:
两个日期对象,第一个为当前日期对象的年月日,第二个为待比较日期对象的年月日
输出格式:
当前日期对象的信息,当前对象和待比较日期对象的比较结果
输入样例:
在这里给出一组输入。例如:
2008 6 12 2009 6 22
输出样例:
在这里给出相应的输出。例如:
6/12/2008 -1
通过代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
MyDate d1 = new MyDate(sc.nextInt(), sc.nextInt(), sc.nextInt());
MyDate d2 = new MyDate(sc.nextInt(), sc.nextInt(), sc.nextInt());
d1.print();
System.out.print(" ");
System.out.println(d1.compare(d2));
sc.close();
}
}
class MyDate{
private int y, m, d;//年、月、日
public MyDate(int y, int m, int d) {
this.y = y;
this.m = m;
this.d = d;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getM() {
return m;
}
public void setM(int m) {
this.m = m;
}
public int getD() {
return d;
}
public void setD(int d) {
this.d = d;
}
public void print() {
System.out.print(this.m+"/"+this.d+"/"+this.y);
}
int compare(MyDate d) {
if(this.y < d.getY()) return -1;
else if(this.y > d.getY()) return 1;
if(this.m < d.getM()) return -1;
else if(this.m > d.getM()) return 1;
if(this.d < d.getD()) return -1;
else if(this.d > d.getD()) return 1;
return 0;
}
}