Lab 2: Interfaces
Objectives
- Translate UML class diagrams into source code
- Construct software which uses an interface
- Employ aggregation and composition in a software system
- Use the
java.util.ArrayList
collection class to hold class data
Resources
- Accept the GitHub Classroom assignment invitation in Canvas and then, in IntelliJ, create a new project from Version Control using the repository URL.
- You will need to use a two-dimensional array in this lab. Here is a brief tutorial.
Introduction
In this lab, you will explore using interfaces to represent items on a calendar.
The Schedulable
interface represents an item that can appear on one's calendar.
It can represent a meeting, a class (or section), or even an entire calendar.
The Meeting
, Section
, and Calendar
classes all implement the Schedulable
interface. The primary abilities of something that is Schedulable
are that you
can see if it is busy at a particular time, add to or remove from the schedule, and
get the schedule's name.
Assignment Details
A class diagram for the program is shown in the figure below:
You will implement all of the classes shown in the diagram.
The class constants in the interface are used to represent different days of the week. You should use these constants instead of integer values when specifying a day. Each method should behave as follows.
boolean isBusy(int day, int hour)
— returnstrue
if and only if something is already scheduled at this day and hour.boolean add(Schedulable item)
— Adds the item to this schedulable item and returnstrue
if it was successful. Depending on the implementation of this interface, this method may always returnfalse
(in the case ofMeeting
), always returntrue
(in the case ofSection
andCalendar
).boolean remove(Schedulable item)
removes the item from the object, if it exists, and returnstrue
if and only if the object is modified (the item is found and removed).String getName()
returns the name for the object.
Specifics for Different Implementations
The Meeting
class represents the simplest implementation of Schedulable
. A meeting represents one schedulable item that could appear on a calendar. It is one contiguous time slot that is represented by a starting time (hour
), duration, and day. Both add()
and remove()
do nothing other than returning false
, and the getName()
method returns X.
The Section
class represents a collection of meetings representing a course section. The Driver
class demonstrates creating a section that consists all of the meeting times for a class. Each of the items added to the object are stored in the items
instance variable. The constructor uses Java's varargs feature to allow one to pass zero or more schedulable items to the constructor.
Section
.
Section section = new Section("Empty"); section = new Section("OneCredit", new Meeting(8, 1, MONDAY)); section = new Section("LectureOnly", new Meeting(8, 1, MONDAY), new Meeting(8, 1, WEDNESDAY), new Meeting(8, 1, FRIDAY));
All of the Schedulable
items passed are stored in an array called items
.
In the first case, the array is empty; In the second case, the array has one element; and in
the third case, the array has three elements.
The Calendar
class represents a person's entire calendar. Sections and meetings can all be added to the calendar. It operates very similarly to the Section
class, but has two additional methods:
int[][] getScheduleMatrix()
— Returns a two-dimensionally array that represents all of time slots fromstartOfDay
(inclusive) toendOfDay
(exclusive). Cells with a zero in them indicate no scheduled items occur at that time. Non-zero cells indicate that at least one schedulable is on the calendar at that time.String toString()
— Returns a String representing the entire schedule. See sample output below.
Driver Class
The starting repository contains a Driver
class that creates two calendars. You must implement the getStudentCalendar()
that creates a calendar with your schedule for this term. When implemented, your solution should produce output for the two calendars that matches the following (including aligning the columns):
| **Dr. Bukowy** | Mon | Tue | Wed | Thu | Fri |
| -------------- | --- | --- | --- | --- | --- |
| 7:00 | | | | | |
| 8:00 | X | X | X | X | X |
| 9:00 | X | X | X | X | X |
| 10:00 | | | | | |
| 11:00 | | | | | |
| 12:00 | | | | X | |
| 13:00 | | X | X | X | X |
| **Prof. Jones** | Mon | Tue | Wed | Thu | Fri |
| --------------- | --- | --- | --- | --- | --- |
| 8:00 | X | X | X | | X |
| 9:00 | X | X | X | X | X |
| 10:00 | | | | X | |
| 11:00 | | | | | |
| 12:00 | | | | | |
| 13:00 | | X | X | X | X |
| 14:00 | X | X | | X | X |
| 15:00 | | | | | |
| 16:00 | | | | | |
Acknowledgment
This laboratory assignment was developed by Dr. Chris Taylor.