Assignment 4 – inheritance & interfaces
Assignment 4 – Inheritance & Interfaces
Due Sunday, October 14th 23:59PM
Overview
This assignment includes 4 parts + one part for extra credit, covering Lesson 1 – Lesson 10. All
4 parts are for a class called “Appointment” and its 3 subclasses as specified below:
Specifications
• Package name: edu.sjsu.assignment3
• Classes:
o Appointment (superclass)
o OnetimeAppointment (subclass of Appointment)
o DailyAppointment (subclass of Appointment)
o MonthlyAppointment (subclass of Appointment)
Similar to previous assignments, please follow the specifications closely and resolve all compiler errors (or the
autograder can’t grade your submission and you will receive a zero on the assignment). And please put all
classes in one project so it’s easier to submit.
Part 1. Implement Appointment Class (1.5 pts)
Set the Appointment class so that it CANNOT be instantiated but can be subclassed. Then implement the
class as specified below. Note that…
• The start and end dates of an appointment are LocalDate objects. Check the documentation if needed. •
Recall that the constructor and getters can be easily generated by IntelliJ (Alt + Enter and choose what you
want to generate).
• Other than getters, an appointment object has another method called occursOn, which checks whether the
appointment occurs on a date or not. The detailed algorithm depends on the type of appointment. Set
occursOn method so that ANY class inherits the Appointment class MUST implement it.
Specifications
• (private) Instance Variables
o String description
o LocalDate startDate
o LocalDate endDate
• Constructor’ Parameters (types and order should be the same)
o String description, LocalDate startDate, LocalDate endDate
• Methods
o Three getters: getDescription, getStartDate, and getEndDate
o public boolean occursOn(LocalDate date)
Part 2. Implement 3 subclasses (4.5 pts)
Implement 3 subclasses of Appointment class (each is worth 1.5 pts):
OnetimeAppointment, DailyAppointment, and MonthlyAppointment.
Don’t set any instance variables or getters – the subclasses inherit the getters that can access to those
instance variables. All you need to implement are the occursOn method and a constructor.
occursOn (0.75 pt for each subclass)
If you set part 1 occursOn correctly, the compiler will complain if you don’t override occursOn. You can move
your mouse to the class header and click the option “Implement methods” (or Alt + Shift + Enter) for quicker
setup.
Here are the differences between occursOn of the subclasses you need to implement:
• Onetime: check if the input is the exact same as the start date of appointment. •
Daily: check if the input is between the start and end date (inclusive)
• Monthly: check if the input is between the start and end date (inclusive) AND the day of month of the input
date is the same as the start date’s day of month of the appointment
Hint: use the methods in LocalDate class, such as getDayOfMonth, isAfter, isBefore, etc.
And since occursOn in daily and monthly both check if the input is between the start and end date, you can
have a concrete helper method to check this in the Appointment class, which will be inherited and can be used
by its subclasses.
Constructor (0.75 pt for each class)
If you set part 1 occursOn correctly, the compiler will complain if the subclass doesn’t have a constructor. You
can move your mouse to the class header and click “Create constructor matching super” (or Alt + Shift +
Enter) for quicker setup. The constructor must call the super constructor.
For DailyAppointment and MonthlyAppointment, the auto generated constructor is enough.
However, for a one-time appointment, the starting date and the ending date should be the same.
Therefore, modify the generated constructor of the OnetimeAppointment class so that it only takes a
String for description and a LocalDate for the starting date.
Specifications
• Constructor’ Parameters (types and order should be the same)
o String description, LocalDate startDate, LocalDate endDate
o (For OnetimeAppointment class ONLY) String description, LocalDate startDate •
Method
o public boolean occursOn(LocalDate date)
See examples in the next page.