-1

在我的 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;
    }
    }

这是事件类

    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]);
            }
        }
    }
4

2 回答 2

2

well, your event class has a method:

public boolean equals(Object obj)

Which, presumably, should return whether or not the passed event is equal to the instance.

So your void removeEvent(Event event) method should look similar to the following:

take note that this is psudo-code and not valid java. you're going to have to flesh out the details on your own.

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

The rest of the methods are going to more or less be similar in concept to the first one with 2 varying factorrs:

  • how you identify a match
  • what you do with the match
于 2012-09-06T21:29:41.497 回答
1

Since this is homework, I don't actually want to do your homework. So as a hint, you want to use (your event).equals(comparing to other event), not "==".

于 2012-09-06T21:29:12.667 回答