Create an abstract class “Appointment” that represents an event on a calendar. T
ID: 3715059 • Letter: C
Question
Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter and setter for each of the four instance variables • An abstract method named occursOn which returns a boolean and takes three parameters of type int: aYear, aMonth, aDay • Overrides the toString method from the Object superclass which returns the String: "Appointment: aDescription On Date: 1/1/1969" where, “aDescription” is the value stored in the description instance variable and “1/1/1969” are the values stored in the month, day, and year instance variables respectively. • Overrides the equals method from the Object superclass which takes a single parameter of type “Object” and returns true if the given objects is an “Appointment” object and it’s year, month, and day instance variables are equal to the current objects instance variables. If the given parameter is null, equals should return false. Create a class “Onetime” which subclasses the “Appointment” class. The “Onetime” class will define no new instance variables. Write a parameterized constructor which takes three parameters of type int: aYear, aMonth, aDay. The parameterized constructor sets the “year”, “month”, and “day” instance variables defined in the “Appointment” class to the values passed as parameters. The parameterized constructor should set the “description” instance variable to an empty String. ("") The “Onetime” class must also implement the following methods: • It must implement the abstract method “occursOn” defined in the “Appointment” class. “occuresOn” should return true if the given parameters match the year, month, and day instance variables for the object. Otherwise it should return false. Create a class “Yearly” which subclasses the “Appointment” class. The “Yearly” class will define no new instance variables. Write a parameterized constructor which takes two parameters of type int: aMonth and aDay. The parameterized constructor sets the “month” and “day” instance variables defined in the “Appointment” class to the values passed as parameters. The parameterized constructor should set the “description” instance variable to an empty String. ("") The “Yearly” class must also implement the following methods: • It must implement the abstract method “occursOn” defined in the “Appointment” class. “occuresOn” should return true if the given parameters match the month and day instance variables for the object. (We can ignore the aYear parameter because this event repeats yearly so every value is correct) Otherwise it should return false. • Overrides the toString method from the Object superclass which returns the String: "Appointment: aDescription Every Year On 1/1" where, “aDescription” is the value stored in the description instance variable and “1/1” are the values stored in the month and day instance variables respectively. Create a class “Monthly” which subclasses the “Appointment” class. The “Monthly” class will define no new instance variables. Write a parameterized constructor which takes one parameter of type int: aDay. The parameterized constructor sets the “day” instance variable defined in the “Appointment” class to the value passed as a parameter. The parameterized constructor should set the “description” instance variable to an empty String. ("") The “Monthly” class must also implement the following methods: • It must implement the abstract method “occursOn” defined in the “Appointment” class. “occuresOn” should return true if the given parameters match the day instance variable for the object. (We can ignore the aYear and aMonth parameters because this event repeats monthly so every value is correct) Otherwise it should return false. • Overrides the toString method from the Object superclass which returns the String: "Appointment: aDescription Every Month On 1" where, “aDescription” is the value stored in the description instance variable and “1” is the values stored in the day instance variable respectively.
Explanation / Answer
abstract class Appointment //abstract base class
{
private int year,month,day;
private String description;
//set and get methods
public void setYear(int year)
{
this.year = year;
}
public int getYear()
{
return year;
}
public void setMonth(int month)
{
this.month = month;
}
public int getMonth()
{
return month;
}
public void setDay(int day)
{
this.day = day;
}
public int getDay()
{
return day;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return description;
}
public abstract boolean occursOn(int aYear,int aMonth,int aDay);
public String toString()
{
return "Appointment: "+getDescription()+" On Date: "+month +"/"+day+"/"+year;
}
public boolean equals(Appointment app)
{
if(this.day == app.day && this.month == app.month && this.year == app.year)
return true;
else
return false;
}
}
//derived classes
class Onetime extends Appointment
{
public Onetime(int aYear,int aMonth,int aDay)
{
setYear(aYear);
setMonth(aMonth);
setDay(aDay);
setDescription("");
}
public boolean occursOn(int aYear,int aMonth,int aDay)
{
if(this.getYear() == aYear && this.getMonth() == aMonth && this.getDay() == aDay)
return true;
else
return false;
}
public String toString()
{
return "Appointment Onetime: "+getDescription()+getYear()+"/"+getMonth()+"/"+getDay();
}
}
class Yearly extends Appointment
{
public Yearly(int aMonth,int aDay)
{
setMonth(aMonth);
setDay(aDay);
setDescription("");
}
public boolean occursOn(int aYear,int aMonth,int aDay)
{
if( this.getMonth() == aMonth && this.getDay() == aDay)
return true;
else
return false;
}
public String toString()
{
return "Appointment Yearly: "+getDescription()+" Every Year On "+getMonth()+"/"+getDay();
}
}
class Monthly extends Appointment
{
public Monthly(int aDay)
{
setDay(aDay);
setDescription("");
}
public boolean occursOn(int aYear,int aMonth,int aDay)
{
if( this.getDay() == aDay)
return true;
else
return false;
}
public String toString()
{
return "Appointment Monthly: "+getDescription()+" Every Month On 1";
}
}
class Test
{
public static void main (String[] args)
{
Appointment[] a = new Appointment[3];
a[0] = new Onetime(2017,10,31);
a[1] = new Yearly(12,7);
a[2] = new Monthly(31);
for(int i=0;i<3;i++)
{
System.out.println(a[i]);
}
}
}
Output:
Appointment Onetime: 2017/10/31
Appointment Yearly: Every Year On 12/7
Appointment Monthly: Every Month On 1
Do ask if any doubt. Please upvote
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.