+1 8108305898 essaysprings@gmail.com

Java program

Java program
In this assignment you are to write classes for a simple calendar application. More specifically, you should write the following three classes: Date.java: For representing the dates/days. This is the simplest building block for the next two classes. Event.java: For representing an event. Events can be put into a calendar. Since multiple Events can be put in one Calendar, we will use an array to do so. Calendar.java: For storing a collection of events. We can add, remove, and determine if an event exists in a calendar. Every data field should be declared private, and every method should be declared public unless otherwise stated. Class Date Class Definition: public class Date implements Comparable { //Data fields and Methods } Data Fields:Class Date has three data fields: int year (must be between 2021-2080) int month (must be between 1-12) int day (must be between 1-31) To keep things simple, we will not concern ourselves with determining if the year is a leap year or checking to make sure the month and day are okay. For instance, checking that the day is less than or equal to 30 for months with only 30 days. While we could add these stipulations the goal of this lab is to just get back into Java programming. Constructors:Class Date has one constructor which initializes year, month, and day. The signature for this constructor is as follows: public Date(int year, int month, int day) throws IllegalArgumentException { //Constructor body } You should check to see if each argument is within the specified range or throw an IllegalArgumentException otherwise. (If you don’t remember how to perform Exception handling in Java then watch the following video on YouTube: http://www.youtube.com/watch?v=-aQeyTGqoQc) Note: The names of the arguments are the same as the data fields. To set the data fields using the arguments you will need to use the keyword this. For instance, this.year = year. The keyword this refers to the instance of the object. To get you started, here is the first check and set for the data field year. public Date(int year, int month, int day) { if(year < 2021 || year > 2080) { throw new IllegalArgumentException(“Year must be between 2021 and 2080”); } /*Checks for month and day*/ this.year = year; /*Set month and day here*/ } 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 returns true if this Date object has the same year, month, and day as obj. Otherwise, it returns false. Some additional checks that should be made should be to return false if obj is null or is not an instance of Date. If obj is the caller, then return true (no need to check the data fields). If obj does not fit these criteria, then it needs to be coerced into a Date object before it’s fields can be compared correctly. Code for these conditions looks as follows. public boolean equals(Object obj) { //If obj is null, return false if(obj == null) { return false; } //If obj is not an instance of Date //return false if(!(obj instanceof Date)) { return false; } //If obj is this object return true if(this == obj) { return true; } //Coerce obj into a Date object //so the data fields can be accessed. Date otherDate = (Date) obj; /*Perform necessary comparisons*/ } Now compare this Date object with “otherDate” object. Note: While you can do multiple if statements to check each data field, you only need to add 1 more line with a return statement to complete the task. Don’t forget about your Boolean operators. int compareTo(Date otherDate): This method is inherited from Comparable interface and must return 0 if this Date object is equal to the otherDate object -1 if this Date object is earliear than the otherDate object +1 if this Date object is later than the otherDate object The compareTo method is the trickiest one in the Date class so let us think about it using a thought experiment. Consider you and another individual both reach in blindly to a bin of calendars. You each flip open the calendar to a random spot and point to a day. How would you compare each pointed at date to determine whose date comes before, after, or is the same as the other? First you might check the years. If your year came after the other person’s year, then you would say 1. If yours came before theirs then you would say -1. If the years were the same, you would move onto the months and do the same checks. If the months were the same, you would move to the days. If those were the same then you must have pointed to the same year, month, and day and would say 0. Keep in mind, also, that the values +1, -1, and 0 are relative to the caller. For example, suppose we have the following date1.compareTo(date2); If a +1 is returned, then this means date1 > date2 or date1 comes after date2. If -1 is returned, then date1 < date2 or date1 comes before date2. If 0 is returned, then date1 is the same as date2. Class Event Class Definition: public class Event { //Data 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 end: The end time/hour of the event. It must be a number between 0-23 String description: A description of the event public Event(Date date, int start, int end, String description) throws IllegalArgumentException{ //constructor body } Make sure start and end are with the correct range defined above. You should also make sure start <= end. In other words, start and end can be the same, but start cannot be greater than end. If start or end are not within the correct ranges or start > end, then an IllegalArgumentException should be thrown. 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/year start — end: description”.Tip: Events already have an object to generate the “month/day/year” portion of the above string. You just need to make sure to call the method on that object that does this. 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 compare dates and description).Much like the Date class, the equals method should verify that the obj argument is not null and is an instance of Event otherwise return false. Also, return true if this == obj. If these fail, then you need to convert obj to an Event object and do the necessary checks. Note: DO NOT COMPARE THE STRING OBJECTS OR THE DATE OBJECTS WITH ==. If you do not know why then you should review how to do proper value comparisons of objects. Class Calendar public class Calendar{ //Date fields and Methods } Data Fields: static final int MAXEVENTS=4: A constant representing the maximum number of events that can be stored. Make it just 4 to support easy testing! Event[] events: An array holding the scheduled events, of size MAXEVENTS. If you don’t remember how to initialize an array and access its elements, please refer to this Oracle Tutorial: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html int numEvents: The number of events currently scheduled (i.e., the number of entries in the “events” array). Constructors: Class Calendar has one default constructor which creates an empty array of events of size MAXEVENTS, and initializes numEvents to zero. public Calendar(){ //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 the array is not already full, you should traverse the array and insert the event in place of the first null entry. Don’t forget to increment the “numEvents” if an event is added successfully. int findEvent(Event e): traverses the “events” array to look for an event that is equal to the object “e”. If such an event is found, then the index of it in the array is returned. Otherwise, “-1” is returned. Note that the array may contain null entries so before checking for equality, make sure that the array entry is not null otherwise your program will throw a NullPointerException. boolean removeEvent(Event e): Removes an event from the array and returns true if the remove operation is successful and false otherwise. Your method should first call findEvent to retrieve the index of the array entry that contains the event. If findEvent returns -1 then the event does not exist in the array 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. all non-null entries in the “events” array). Each event should be printed in a separate line. As you write the classes you should be testing your code with the provided test class, CalendarTest.java. The tests are given to help you determine how correct your code is. DO NOT CHANGE THE CODE FOR THE TESTS. IF YOU SUSPECT A TEST TO BE INCORRECT, PLEASE LET ME KNOW ASAP. I WILL VERIFY AND CORRECT. These are the tests I will be using to grade your work. If you change them then you risk getting points off because they fail the original tests. What you should turn in: Submit a zip archive of your project folder. The zip file should be named your netID.zip. For instance, if your netID is broge2 then your zip archive should be broge2.zip. Please do not hesitate to email me if you have any questions. Grading Rubric: Classes, data fields, and accessor/mutator methods are properly defined. This includes Correct implementation Correct access modifier. Do not just leave blank or public. Consider what the modifier should be. Correct name 20% Constructors and other methods pass all the tests 80%
Java program
/** * A test class for testing “Date” “Event”, and “Calendar” classes */ 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.out.println(“****Fails-no Exception thrown”); } catch (Exception e) { System.out.println(“****passes”); } System.out.println(“Trying invalid date–month”); try { Date d1 = new Date(2024, 13, 28); System.out.println(“****Fails-no Exception thrown”); } catch (Exception e) { System.out.println(“****passes”); } System.out.println(“Trying invalid date–day”); try { Date d1 = new Date(2024, 12, 0); System.out.println(“****Fails-no Exception thrown”); } catch (Exception e) { System.out.println(“****passes”); } System.out.println(“Trying valid date”); try { Date d1 = new Date(2025, 8, 28); System.out.println(“****passes”); } catch (Exception e) { System.out.println(“****Fails-exception thrown”); } System.out.println(“Testing the equals method”); Date d1 = new Date(2024, 8, 28); Date d2 = new Date(2024, 8, 28); System.out.println(“Trying for two equal dates”); if (d1.equals(d2)) System.out.println(“****passes”); else System.out.println(“****Fails”); System.out.println(“Trying for two unequal dates”); d1 = new Date(2024, 8, 28); d2 = new Date(2024, 8, 16); if (d1.equals(d2)) System.out.println(“****fails”); else System.out.println(“****passes”); System.out.println(“Tesing the compareTo method”); d1 = new Date(2025, 8, 28); d2 = new Date(2024, 8, 16); System.out.println(“trying for different years”); if (d1.compareTo(d2) > 0) System.out.println(“****passes”); else System.out.println(“****fails”); d1 = new Date(2024, 9, 28); d2 = new Date(2024, 8, 16); System.out.println(“trying for equal years, but different months”); if (d1.compareTo(d2) > 0) System.out.println(“****passes”); else System.out.println(“****fails”); d1 = new Date(2024, 8, 28); d2 = new Date(2024, 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.out.println(“****fails”); d1 = new Date(2024, 8, 28); d2 = new Date(2024, 8, 28); System.out.println(“trying for equal years, month, and day”); if (d1.compareTo(d2) == 0) System.out.println(“****passes”); else System.out.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.out.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.out.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.out.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:n8/28/2024 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.out.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.out.println("Failed"); System.out.println("Trying to remove an existing event"); if (c.removeEvent(e3)) System.out.println("Passes"); else System.out.println("Fails"); System.out.println("tring to remove a non-existing event"); if (c.removeEvent(e3)) System.out.println("Fails"); else System.out.println("Passes"); System.out.println("Testing dump"); System.out.println("your program should print:"); System.out.println("8/28/2024 10--12:event 1n8/28/2024 12--14:event 2n8/28/2024 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/2024 10--12:event 1n8/28/2024 12--14:event 2n8/28/2024 13--14:event 5n8/28/2024 14--15:event 4"); System.out.println("This is what your program printed: "); c.dump(); }