The goals of this lab are to learn how to design methods for classes in Java-like language and to understand the power of the dynamic dispatch. Of course, we will follow the design recipe, and that means we need to learn how to design tests for the methods we define.
Start the lab by downloading the file Calendar.java. Build a project named Calendar and import the file Calendar.java into it. Add the tester.jar to the classpath, set us a new RunConfiguration that allows you to run the program, and run the program.
Note: When using the Browse button to search for the main, Eclipse will revert the name of the project to the first project that used tester.Main as its starting class. Make sure you change it back to your project name before hitting Apply or Run.
The file shows you how we like to organize the code, write the templates for each class, write the tests for each class. However, it does not have any meaningful code for any of the methods defined there --- the body of each method is only a stub, a place-holder that produces a useless value of the desired type, so that the program can be compiled and run. As you can see, three out of four tests fail.
Read the code and make sure you understand what the program is trying to accomplish. The goal is to build a program that handles your online calendar. (Of course, we simplify it, so that the amount of work would be manageable.)
Look at the class diagram at the bottom and make sure how the various classes relate to each other. Read the method headers and make sure you understand what each method is supposed to do.
Method toMinutes in the class ClockTime should convert the hours and minutes into the total minutes since midnight on this day
Method duration in the class Event should compute the duration of this event in minutes
Method scheduledTime for the union of classes ISchedule computes the total time during the day that is already taken up by scheduled events.
Add an example of two more events and make a new schedule that includes these events. Then add tests for the methods for these new examples.
Complete the body of the method toMinutes in the class ClockTime.
Complete the body of the method duration in the class Event. Notice how the template helps you to see what is already available and how the rest is easy.
Complete the body of the method scheduledTime for the union of classes ISchedule.
Note: You do not need to deal with overlapping events at this time.
At this point you should understand how the methods are designed, see that what you have learned in Fundies 1 translates directly to the design of method in this new language.
You will now design several methods on your own. As you design new methods, make sure to update the template for each class that will be affected.
When designing a method, copy the template from its fixed position, add to the template any new information based on the arguments the method consumes, then design the method. Once you have the method body, you can delete the template you have used.
Design the method isBefore in the class ClockTime that determines whether this time on the clock is earlier than the given time.
Note: If the times are the same, return true.
Design the method endsBefore in the class Event that determines whether this event ends before the given event starts.
Design the method overlapHuh in the class Event that determines whether this event overlaps with the given event (and you would need to miss a part of one of them).
Design the method goodScheduleHuh for the union of classes ISchedule that determines whether the events in this schedule are ordered according to their starting time, and no events overlap.
Design the method freeTime for the union of classes ISchedule that produces a new ISchedule of all the free times in your day with this schedule. Assume that the schedule that invokes this method is a good schedule according to the method goodSchedule.