Java——MyDate类

Description

构造日期类MyDate类,包含年月日,提供相应的get和set函数,提供void print()函数打印日期,提供int compare(MyDate d)测试当前对象和参数对象d的早晚,如果早则返回-1,晚则返回1,相等则返回0
在main函数中,读入两个日期对象,输出第一个日期对象的信息,输出两个对象的比较结果

Input

两个日期对象,第一个为当前日期对象的年月日,第二个为待比较日期对象的年月日

Output

当前日期对象的信息,当前对象和待比较日期对象的比较结果

Sample Input

2008 6 12 2009 6 22

Sample Output

6/12/2008 -1
import java.util.Scanner;

public class Main{

    public static void main(String[] argc)

    {

        Scanner scan = new Scanner(System.in);

        int Year = scan.nextInt();

        int Month = scan.nextInt();

        int Day = scan.nextInt();

        

        int AnotherYear = scan.nextInt();

        int AnotherMonth = scan.nextInt();

        int AnotherDay = scan.nextInt();

        scan.close();

        

        MyDate firstDay = new MyDate(Year,Month,Day);

        MyDate secondDay = new MyDate(AnotherYear,AnotherMonth,AnotherDay);

        

        firstDay.print();

        System.out.print(firstDay.compare(secondDay));

    }

    

}

class MyDate{

    private int year;

    private int month;

    private int day;

    

    public MyDate(int year,int month,int day)

    {

        this.year = year;

        this.month = month;

        this.day = day;

    }

    public void setYear(int y)

    {

        year = y;

    }

    public void setMonth(int m)

    {

        month = m;

    }

    public void setDay(int d)

    {

        day = d;

    }

    public int getYear()

    {

        return year;

    }

    public int getMonth()

    {

        return month;

    }

    public int getDay()

    {

        return day;

    }

    

    public void print()

    {

        System.out.print(month+"/"+day+"/"+year+" ");

    }

    public int compare(MyDate d)

    {

        if(d.year > this.year)

        {

            return -1;

        }

        else if(d.year == this.year)

        {

            if(d.month > this.month)

            {

                return -1;

            }

            else if(d.month == this.month)

            {

                if(d.day > this.day)

                {return -1;}

                else if(d.day == this.day)

                {

                    return 0;

                }

                    

            }

                

        }

        return 1;

    }

    

}

 

1.编写MyDate.java 2.该有如下构造方法 2.1 无参数构造方法public MyDate(),以当前的系统时间构造MyDate对象 2.2 public MyDate(int year, int month, int day), 以指定的年月日构造MyDate对象 3.该有如下属性 3.1 private int year ;//年 3.2 private int month; //月 3.3 private int day; //日 4.该有如下方法 4.1 public String after(int day); //返回当前对象代表的日期之后day天的日期,比如当前对象是2008-8-8,调用after(5)以后,应该返回2008-8-13,格式可自定义 4.2 public String before(int day); //返回当前对象代表的日期之前day天的日期,比如当前对象是2008-8-8,调用before(5)以后,应该返回2008-8-3,格式可自定义 4.3 public void setYear(int year); //设置年为指定值year 4.4 public void setMonth(int month); //设置月为指定值month 4.5 public void setDay(int day); //设置日为指定值day 4.6 public int getYear(); //返回当前对象的年 4.7 public int getMonth(); //返回当前对象的月 4.8 public int getDay(); //返回当前对象的日 4.9 public void set (int year, int month, int day); //设置年、月、日为指定的值year、month、day 4.10 public String toString();//以字符串形式返回当前对象的年月日,例如2008年08月08日,格式可自定义 4.11 public boolean equals(MyDate mydate);//当前对象与另一个对象比较,如果两个对象的年月日均相等,则返回true,反之返回false 5.编写TestMyDate.java,在main方法中对MyDate的各个方法进行测试 6.按照编程规范为MyDate.java编写规范的代码 7.按照java doc API的要求,对代码编写规范的注释,然后利用javadoc.exe命令生成MyDate.java的API doc 8.撰写上机报告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值