Question: Java: Write classes for a simple calendar application. More specifically, you should write the following three classes: Date.java : for representing the dates/days Event.java :
Java: Write classes for a simple calendar application. More specifically, you should write the following three classes: Date.java: for representing the dates/days Event.java: for representing an event. Calendar.java: for storing a set of events
The specification for each class is as follows:
------------------------------------------------------------------------------- **Date.java**
Class Definition: Class Date should implement the Comparable interface:
publicclassDate implementsComparable { //Data fields and Method
}
Data Fields: Class Date has three data fields: int year (must be between 2014-2020) int month(must be between 1-12) int day(must be between 1-31)
Constructors: Class Date has one constructor which initializes year, month, and year. The signature for this constructor is as follows:
publicDate(int year, intmonth, int day) throwsIllegalArgumentException { //constructor body
}
You should check to see if each argument is within the specified range and throw an IllegalArgumentException otherwise.(If you dont remember the exception handling in java watch this complete tutorial on youtube: http://www.youtube.com/watch?v=-aQeyTGqoQc )
Methods: Accessors for all three data fields: int getYear() , int getMonth(), int getDay() String toString(): produces a string version of the date which should be in the form month/day/year Boolean equals( Object obj): This method overrides the equals method in the Object class. It returnstrue if this Date object has the same year, month and day as obj. Otherwise, it returns false. Beforechecking for equality obj must be casted to Date as follows: publicBoolean equals(Object obj){Date otherDate = (Date)obj; //the rest of the method body} Now you can compare this Date object with otherDate object. public int compareTo(Date otherDate): This method is inherited from Comparable interface and must retur * 0 if this Date object is equal to the otherDate object * -1 if this Date object is earlier than the otherDate object * +1 if this Date object is later than the otherDate object
------------------------------------------------------------------------------- **Event.java**
Class Definition: publicclassEvent {
//Date fields and Methods
}
Data Fields: Date date: ( the date of the event) int start: (The start time/hour of the event. It must be a number between 0-23) int day: (the end time/hour of the event. It must be a number between 0-23) String description: ( A description of the event.)
Constructors: Class Event has one constructor which initializes Date, start, end, and description. The signature for this constructor is as follows:
publicEvent (Date date, intstart, intend, String description) throws IllegalArgumentException{ //constructor body }
You should check to see if start hour is less than or equal end hour and throw an IllegalArgumentException otherwise.
Methods: Accessors for all three fields: Date getDate(), int getStart(), int getEnd(), String getDescription(). A Mutator for the description field: void setDescription(String newDescription) String toString(): Produces a string version of an Event which should be of the form: "month/day/yearstart--end: description". Boolean equals(Object obj) Determines whether two Event objects represent the same event. True if both event objects have the same date, start, end, and description attributes (use equals to comparedates and description).
------------------------------------------------------------------------------- **Calendar.java**
publicclassCalendar {
//Date fields and Methods }
Data Fields: static final int MAXEVENTS=4 : A constant representing the maximum number of events that can bestored. Make it just 4 to support easy testing! Event[] events: An array holding the scheduled events, of size MAXEVENTS. int numEvents The number of events currently scheduled (i.e., the number of entries in the eventsarray).
Constructors: Class Calendar has one default constructor which creates an empty array of events of size MAXEVENTS, and initializes numEvents to zero.
publicCalendar() { //Constructor Body }
Methods: Boolean addEvent(Event e): Adds an event to the events array if array is not full(numEvents==MAXEVENTS). It returns true if the event is successfully added and false otherwise. If thearray is not already full, you should traverse the array and insert the event in place of the first null entry.Dont forget to increment the numEvents if an event is added successfully. int f indEvent(Event e):traverses the events array to look for an event that is equal to e. If such eventis found, then the index of it in the array is returned. Otherwise, -1 is returned. Note that the array maycontain null entries so before checking for equality, make sure that the array entry is not null otherwiseyour program may throw a NullPointerException.Boolean removeEvent(Event e):removes an event from the array and returns true if the removeoperation is successful and false otherwise. Your method should first call FindEvent to retrieve the indexof the array entry that contains the event. If FindEvent returns -1 then the event does not exist in thearray and your method should return false. Otherwise, your method should set the array entry to null. Void dump(): Prints all the events in the calendar (i.e. allnon-null entriesin the events array). Eachevent should be printed in a separate line.
After you are done with writing the above three classes. Use the CalendarTest.java class to test your three classes. This is the test harness I use for grading your program. Make sure that your classes pass all the tests. What you should turn in:You should turn in three java files: Date.java, Event.java, and Calendar.java.
------------------------------------------------------------------------------- **CalendarTest.java**
public class CalendarTest { public static void main(String [] args) { System.out.println("**********************Testing the Date class**********************: "); System.out.println("Testing the constructor"); System.out.println("Trying invalid date--year"); try { Date d1= new Date(2012,8,28); System.err.println("****Fails-no Exception thrown"); } catch(Exception e) { System.out.println("****passes"); } System.out.println("Trying invalid date--month"); try { Date d1= new Date(2014,13,28); System.err.println("****Fails-no Exception thrown"); } catch(Exception e) { System.out.println("****passes"); } System.out.println("Trying invalid date--day"); try { Date d1= new Date(2014,12,0); System.err.println("****Fails-no Exception thrown"); } catch(Exception e) { System.out.println("****passes"); } System.out.println("Trying valid date"); try { Date d1= new Date(2015,8,28); System.out.println("****passes"); } catch(Exception e) { System.err.println("****Fails-exception thrown"); } System.out.println("Testing the equals method"); Date d1= new Date(2014, 8, 28); Date d2 = new Date(2014, 8, 28); System.out.println ("Trying for two equal dates"); if (d1.equals(d2)) System.out.println("****passes"); else System.err.println("****Fails"); System.out.println("Trying for two unequal dates"); d1= new Date(2014, 8, 28); d2 = new Date(2014, 8, 16); if (d1.equals(d2)) System.err.println("****fails"); else System.out.println("****passes"); System.out.println("Tesing the compareTo method"); d1= new Date(2015, 8, 28); d2 = new Date(2014, 8, 16); System.out.println("trying for different years"); if (d1.compareTo(d2)> 0) System.out.println("****passes"); else System.err.println("****fails"); d1= new Date(2014, 9, 28); d2 = new Date(2014, 8, 16); System.out.println("trying for equal years, but different months"); if (d1.compareTo(d2)> 0) System.out.println("****passes"); else System.err.println("****fails"); d1= new Date(2014, 8, 28); d2 = new Date(2014, 8, 16); System.out.println("trying for equal years and months, but different days"); if (d1.compareTo(d2)> 0) System.out.println("****passes"); else System.err.println("****fails"); d1= new Date(2014, 8, 28); d2 = new Date(2014, 8, 28); System.out.println("trying for equal years, month, and day"); if (d1.compareTo(d2)== 0) System.out.println("****passes"); else System.err.println("****fails"); /**************************************************************************************************/ System.out.println("**********************Testing the Event class*****************************"); System.out.println("Testing the constructor"); System.out.println("Trying invalid event start greater than end"); try { Event e1= new Event(d1,14, 12, "some events"); System.err.println("****Fails-no Exception thrown"); } catch(Exception e) { System.out.println("****passes"); } System.out.println("Testing the equals methdod"); Event e1= new Event(d1, 10,12, "some events"); Event e2 = new Event(d1, 10,12, "some events"); System.out.println ("Trying for two equal events"); if (e1.equals(e2)) System.out.println("****passes"); else System.err.println("****Fails"); System.out.println("Trying for two unequal events"); e1= new Event(d1, 10, 12, "event 1"); e2= new Event(d1, 12, 14, "event 2"); if (e1.equals(e2)) System.err.println("****fails"); else System.out.println("****passes"); /*****************************************************************************************************/ System.out.println("******************Testing the Calendar Class********************"); System.out.println("Trying to add an event to an empty calendar"); Calendar c= new Calendar(); c.addEvent(e1); System.out.println("Your program should print: 8/28/2014 10--12:event 1" ); System.out.println("This is what your program printed: "); c.dump(); c.addEvent(e2); Event e3 = new Event(d1, 14, 15, "event 3"); Event e4= new Event (d2, 14, 15, "event 4"); c.addEvent(e3); c.addEvent(e4); System.out.println("Trying to add to a full array"); Event e5 = new Event (d2, 13, 14, "event 5"); if (c.addEvent(e5)) System.err.println("Fails. The arra is full but addEvent returns true"); else System.out.println("Passes. addEvent returns false"); System.out.println("Trying to find an existing event at the end of the array"); if (c.findEvent(e4)==3) System.out.println("Passes"); else System.out.println("Failed"); System.out.println("Trying to find an existing event at the beginning of the array"); if (c.findEvent(e1)==0) System.out.println("Passes"); else System.out.println("Failed"); System.out.println("Trying to find a non-existing existing event"); if (c.findEvent(e5)<0) System.out.println("Passes"); else System.err.println("Failed"); System.out.println("Trying to remove an existing event"); if (c.removeEvent(e3)) System.out.println("Passes"); else System.err.println("Fails"); System.out.println("tring to remove a non-existing event"); if (c.removeEvent(e3)) System.err.println("Fails"); else System.out.println("Passes"); System.out.println("Testing dump"); System.out.println("your program should print:"); System.out.println("8/28/2014 10--12:event 1 8/28/2014 12--14:event 2 8/28/2014 14--15:event 4"); System.out.println("This is what your program printed: "); c.dump(); System.out.println("Adding an event to a non-full calendar:"); if (c.addEvent(e5)) System.out.println("Passes"); else System.out.println("Fails"); System.out.println("Testing dump"); System.out.println("your program should print:"); System.out.println("8/28/2014 10--12:event 1 8/28/2014 12--14:event 2 8/28/2014 13--14:event 5 8/28/2014 14--15:event 4"); System.out.println("This is what your program printed: "); c.dump(); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
