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;
public class Main{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int y1 = scan.nextInt();
        int m1 = scan.nextInt();
        int d1 = scan.nextInt();
        MyDate p =  new MyDate(y1,m1,d1);
        int y2 = scan.nextInt();
        int m2 = scan.nextInt();
        int d2 = scan.nextInt();
        MyDate d =  new MyDate(y2,m2,d2);
        p.print();
        System.out.println(p.compare(d));
    }
}
class MyDate
{
    private  int  yeah;
    private  int  mouth;
    private  int  day;
    public MyDate(){}
    public MyDate(int _yeah,int _mouth,int _day)
    {
        this.yeah=_yeah;
        this.mouth=_mouth;
        this.day=_day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public void setMouth(int mouth) {
        this.mouth = mouth;
    }

    public void setYeah(int yeah) {
        this.yeah = yeah;
    }

    public int getDay() {
        return day;
    }

    public int getYeah() {
        return yeah;
    }
    public int getMouth() {
        return mouth;
    }
    public void print()
    {
        System.out.print(this.mouth+"/"+this.day+"/"+this.yeah+" ");
    }
    int compare(MyDate d)
    {
        if(this.yeah > d.yeah)
        {
            return 1;
        }
        else if(this.yeah < d.yeah)
        {
            return -1;
        }
        else
        {
            if(this.mouth > d.mouth)
            {
                return 1;
            }
            else if(this.mouth < d.mouth)
            {
                return -1;
            }
            else
            {
                if(this.day > d.day)
                {
                    return 1;
                }
                else if(this.day < d.day)
                {
                    return -1;
                }
                else
                    return 0;
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值