CS 2510 Spring 2012: Lab 3 - Designing Methods for Classes

copyright 2012 Felleisen, Proulx, et. al.

Goals

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.

1  Introduction

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.

  1. 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.)

  2. 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.

    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.

  3. Complete the body of the method toMinutes in the class ClockTime.

  4. 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.

  5. 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.

2  Designing Methods

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.

  1. 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.

  2. Design the method endsBefore in the class Event that determines whether this event ends before the given event starts.

  3. 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).

  4. 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.

  5. 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.

Last modified: Sun Jan 22 20:07:24 EST 2012