In: Computer Science
In Java, implement a superclass Appointment and subclasses
Onetime, Daily, and Monthly. An appointment has a description (for
example, “see the dentist”) and a date. Write a method
occursOn(int year, int month, int day) that checks whether the
appointment occurs on that date. For example, for a monthly
appointment, you must check whether the day of the month
matches.
In Main Class, fill an array of Appointment objects with a mixture of appointments i.e Onetime, Daily, and Monthly (at least 1 from each). Then prompt the user to enter a date and print out all appointments that occur on that date.
Hi,
Please find the answer below:
-------------------------------
Java Program:
package appointment;
/******************************************************************************
*Appointment.java
******************************************************************************/
public class Appointment {
protected String description;
protected int day;
protected int month;
protected int year;
//Appointment Constructor
public Appointment(String desc, int d, int m, int
y)
{
this.description = desc;
this.day = d;
this.month = m;
this.year = y;
}
//getters and setters
public String getDescription() {
return description;
}
public void setDescription(String description)
{
this.description =
description;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
// this method would be overridden in the
subclasses
public boolean occursOn(int d, int m, int y)
{
if(getDay() == d &&
getMonth() == m && getYear() == y)
return
true;
else
return
false;
}
//toString()
public String toString()
{
return description + " on "+ day +
"/" + month + "/" +year ;
}
}
----------------
package appointment;
/******************************************************************************
* OneTime.java
* @author
*
*****************************************************************************/
public class OneTime extends Appointment {
public OneTime(String desc, int d, int m, int y)
{
super(desc, d, m, y);
}
public String toString()
{
return "OneTime Appt -" +
super.toString();
}
}
-----------------
package appointment;
/******************************************************************************
* Daily.java
* @author
*
*****************************************************************************/
public class Daily extends Appointment {
public Daily(String desc, int d, int m, int y)
{
super(desc, d, m, y);
}
//daily events except future events
public boolean occursOn(int d, int m, int y)
{
if(getDay() < d || getYear()
< y || getMonth() < m)
return
true;
else
return
false;
}
public String toString()
{
return "Daily Appt -"+ description
+" from " + getDay() + "/" + getMonth() + "/" + getYear();
}
}
----------------
package appointment;
/******************************************************************************
* Monthly.java
* @author
*
*****************************************************************************/
public class Monthly extends Appointment {
public Monthly(String desc, int d, int m, int y)
{
super(desc, d, m, y);
}
public boolean occursOn(int d, int m, int y)
{
//future events are not
returned
if(getDay() == d &&
(getYear() < y || getMonth() < m))
return
true;
else
return
false;
}
public String toString()
{
return "Monthly Appt - "+
description + " on the day " + day + " of the month from " +
getMonth() + "/" + getYear();
}
}
-------------
package appointment;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int day,month,year;
Appointment[] appointments= new
Appointment[6];
appointments[0]= new OneTime("Get
Together",1,1,2019);
appointments[1]=new Monthly("Pay
Electric bill",1,2,2019);
appointments[2]= new Daily("Get
Milk Packets",1,1,2017);
appointments[3]= new
OneTime("Meeting School Friend",1,1,2020);
appointments[4]=new Monthly("Pay
House Tax",1,3,2019);
appointments[5]= new Daily("Gym
Visit",2,1,2020);
Scanner scanner = new
Scanner(System.in);
System.out.println("Enter date to
show appointments:");
System.out.println("Enter day:
");
day = scanner.nextInt();
System.out.println("Enter month:
");
month = scanner.nextInt();
System.out.println("Enter year:
");
year = scanner.nextInt();
System.out.println("Appointments
are : ");
for(Appointment a : appointments)
{
if(a.occursOn(day, month, year))
System.out.println(a.toString());
}
}
}
-----------------
Program Output:
Enter date to show appointments:
Enter day:
1
Enter month:
1
Enter year:
2021
Appointments are :
Monthly Appt - Pay Electric bill on the day 1 of the month from
2/2019
Daily Appt -Get Milk Packets from 1/1/2017
Monthly Appt - Pay House Tax on the day 1 of the month from
3/2019
Daily Appt -Gym Visit from 2/1/2020
Screenshot:
-------------------------------
Hope this helps.
Let me know if you need more help with this.
Kindly, like the solution if you find it useful
Thanks.