HELP. Your first programming assignment will be for the purpose of reviewing bas
ID: 667257 • Letter: H
Question
HELP.
Your first programming assignment will be for the purpose of reviewing basic Java. Your prior knowledge of Java and the review lectures of module 1 should be sufficient for doing this assignment. 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 (I already got help for this)
• Event.java: for representing an event.
• Calendar.java: for storing a set of events
The Event class
Class Definition: public class Event { //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: public Event (Date date, int start, int end, 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/year start--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 compare dates and description).
The Calendar Class
Class Definition: 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 “e”. If such 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 may 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.
Explanation / Answer
Event.java
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;
}
}
Calendar.java
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) {
if (event.equals (e))
{
return true;
}
else return false;
}
// 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]);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.