Question
Problem 2. Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, see the dentist) and occurs on
Problem 2.Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, see the dentist) and occurs on one or more dates. 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. Then fill an array of Appointment objects with a mixture of appointments. Have the user enter a date and print out all appointments that occur on that date.
For testing, please use the two tester classes provided in the template codes.
Template Code below
abstract Appointment
//HIDE /** A class to keep track of an appointment. */ public abstract class Appointment { private String description;
/** Constructs an appointment without a description. */ public Appointment() { description = ""; }
/** Sets the description of this appointment. @param description the text description of the appointment */ public void setDescription(String description) { this.description = description; }
/** Determines if this appointment occurs on the given date. @param year the year @param month the month @param day the day @return true if the appointment occurs on the given date. */ public abstract boolean occursOn(int year, int month, int day);
/** Converts appointment to string description. */ public String toString() { return description; } }
Monthly
/** Part II. Create the Appointment subclass Monthly. A monthly appointment occurs on the same day every month. */ public class . . . { . . .
}
Onetime class
/** Part II. Create the Appointment subclass Onetime. A onetime appointment occurs on a particular date. */ public class . . . { . . .
}
Daily Class
/** Place the Daily class from Part I here. */
. . .
Appointment Tester 1
//HIDE public class AppointmentTester { public static void main(String[] args) { System.out.print("Monthly is subclass of Appointment: "); System.out.println(Monthly.class.getSuperclass() == Appointment.class); System.out.println("Expected: true"); System.out.print("Onteime is subclass of Appointment: "); System.out.println(Onetime.class.getSuperclass() == Appointment.class); System.out.println("Expected: true"); System.out.print("Monthly appointments have no extra fields:"); System.out.println(Monthly.class.getDeclaredFields().length == 1); System.out.println("Expected: true"); System.out.print("Onetime appointments have no extra fields:"); System.out.println(Onetime.class.getDeclaredFields().length == 3); System.out.println("Expected: true");
// Try some appointments Appointment obj = new Monthly(15, "pay the bills"); System.out.println(obj.occursOn(2016, 8, 15)); System.out.println("Expected: true"); obj = new Onetime(2016, 11, 1, "Dentist appointment."); System.out.println(obj.occursOn(2016, 10, 1)); System.out.println("Expected: false"); System.out.println(obj.occursOn(2016, 11, 1)); System.out.println("Expected: true"); } }
Appointment Tester 2
public class AppointmentTester2 { public static void main(String[] args) { Appointment obj = new Monthly(15, "pay the bills"); System.out.println(obj.occursOn(2016, 8, 15)); System.out.println("Expected: true"); obj = new Onetime(2016, 11, 1, "Dentist appointment."); System.out.println(obj.occursOn(2016, 10, 1)); System.out.println("Expected: false"); System.out.println(obj.occursOn(2016, 11, 1)); System.out.println("Expected: true"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started