7-1 MyDate类 (20 分)
构造日期类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;
class MyDate {
public int y;
public int m;
public int d;
public MyDate(int y, int m, int d) {
this.y = y;
this.m = m;
this.d = d;
}
public void get(int y, int m, int d) {
this.y = y;
this.m = m;
this.d = d;
}
public void set(int y, int m, int d) {
this.y = y;
this.m = m;
this.d = d;
}
public void print() {
System.out.print(m + "/" + d + "/" + y + " ");
}
public int compare(MyDate md) {
int fg = 0;
if (md.y == y & md.m == m & md.d == d) {
fg = 0;
}
if (md.y > y) {
fg = -1;
} else if (md.y < y) {
fg = 1;
} else if (md.y == y) {
if (md.m > m) {
fg = -1;
} else if (md.m < m) {
fg = 1;
} else if (md.m == m) {
if (md.d > d) {
fg = -1;
} else if (md.d < d) {
fg = 1;
} else if (md.d == d) {
fg = 0;
}
}
}
return fg;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int l = input.nextInt();
int w = input.nextInt();
int h = input.nextInt();
MyDate mydate1 = new MyDate(l, w, h);
int ll = input.nextInt();
int ww = input.nextInt();
int hh = input.nextInt();
MyDate mydate2 = new MyDate(ll, ww, hh);
mydate1.print();
System.out.print(mydate1.compare(mydate2));
input.close();
}
}