Java,创建日历

时间:2012-09-06 21:20:19

标签: java calendar

在我的Java类中,我必须构建一个日历应用程序。我已经完成了它,但是我需要几种方法的帮助。我评论了需要帮助的部分。代码包括三个类和一个名为TestCalendar的主程序。我需要帮助的函数位于Calendar类中,名为removeEvent(其中两个,采用两个不同的参数),printEvents和findEvents。提前谢谢!

这是Date类。

            public class Date {
    int year, month, day;
    //constructor
    public Date(int yr, int mth, int dy){
    year = yr;
    if (yr < 2000  || yr > 2100)
    {
        System.out.println("Wrong Calander Year");
        System.exit(1);
    }
    month = mth;
    if (mth < 1 || mth > 12)
    {
        System.out.println("Wrong Month");
        System.exit(1);
    }
    day = dy;
    if (dy < 1 || dy > 31)
    {
        System.out.println("Wrong Day");
        System.exit(1);
    }

    }
    //accessor methods
    public int getYear()
    {
        return year;
    }
    public int getMonth()
    {
        return month;
    }
    public int getDay()
    {
        return day;
    }
    //returns date in correct format
    public String toString()
    {
        return "" + month + "/" + day + "/" + year;
    }
    }

这是Event类

    public class Event {
    Date date;
    int hour;
    String activity;

    Event(int year, int month, int day, int hour, String activity)
    {
        if (year < 2000  || year > 2100)
        {
            System.out.println("Wrong Calander Year");
            System.exit(1);
        }
        if (month < 1 || month > 12)
        {
            System.out.println("Wrong Month");
            System.exit(1);
        }
        if (day < 1 || day > 31)
        {
            System.out.println("Wrong Day");
            System.exit(1);
        }
        this.date = new Date(year, month, day);
        this.hour = hour;
        this.activity = activity;

    }
    public Date getDate()
    {
    return date;    
    }
    public int getHour()
    {
        return hour;    
    }
    public String getActivity()
    {
        return activity;
    }
    void setActivity(String newActivity) 
    {
        this.activity = newActivity;
    }
    public String toString()
    {
    return "" + date +" " + "@" + hour +":" + " " + activity;
    }
    public boolean equals(Object obj)
    {
    if (obj instanceof Event)   
    {
        return true;
    }
    else return false;
    }
    }

日历类

    public class Calander {
        static final int MAXEVENTS = 10;
        Event[] events;
        int numEvents;

        // constructor
        public Calander() {
            numEvents = 0;
            events = new Event[MAXEVENTS];
        }

        void addEvent(int year, int month, int day, int hour, String activity) {
            Event newEvent = new Event(year, month, day, hour, activity);
            events[numEvents] = newEvent;
            numEvents++;
        }


        void removeEvent(int year, int month, int day, int hour, String activity) {


            {
             if (events[numEvents] == null);
             numEvents--;    
            }
        }

        // instructions say to remove (all) Event objects in the Calendar that are equals to the event argument.  Use the equals method from the event class

        void removeEvent(Event event) {

    //what to put here?
        }

        // this method needs to print every Event in the associated Calendar that matches the date arguments.  Print each on a separate line, using the toString method from the Event class.

        void printEvents(int year, int month, int day) { // how to set equality
    if (this.events[numEvents] == )
            {
                // what to put here?
            }
        }

        // same as above but matches the (Date date) arguments
        void printEvents(Date date) {
                toString();
        }

        // Return the first Event in the Calendar that has a matching (equals) activity field.  If no match is found, you must return a reference type, so return null.

                    Event findEvent(String activity) {
                            //what to put here?
            return null;
        }


        void dump() {

            for (int i = 0; i < MAXEVENTS; i++)
            {
                if (events[i] != null)
                System.out.println(events[i]);
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

好吧,你的事件类有一个方法:

public boolean equals(Object obj)

大概应该返回传递的事件是否等于实例。

因此,您的void removeEvent(Event event)方法应与以下内容类似:

注意这是psudo-code而不是有效的java。你将不得不自己充实细节。

void removeEvent(Event event) 
{
    foreach(event e in this.events)
    {
        if(event.equals(e))
        {
            // remove e from the events array
        }
    }
}

其余的方法在概念上或多或少与第一个方法相似,有两个不同的因子:

  • 如何识别匹配
  • 你对比赛做了什么

答案 1 :(得分:1)

由于这是家庭作业,我实际上并不想做你的功课。所以作为提示,你想使用(你的事件).equals(与其他事件相比),而不是“==”。