2006 Time Interval Appointment

<< 2007GameState | APQuestionsTrailIndex | 2006TaxableItem >>

Question from the College Board

This is a quick version... time here is represented merely an integer, to keep the coding quick... in reality, we probably should make a Time Class which has hours and minutes, and define a compareTo method in the time class, rather that just use an integer.... in this version, appointments can only be made on the hour, and we represent 1 PM as 13, and no care is taken about intervals that span across midnight.

TimeInterval

public class TimeInterval
{
  private int beginTime; 
  private int endTime;

  public TimeInterval(int b, int e)
  {
    beginTime = b;
    endTime = e;
  }
  public int getBeginTime(){return beginTime;}
  public int getEndTime(){return endTime;}

  public boolean overlapsWith(TimeInterval interval)
  {
    //Maria Litvin's fancy way in one line: return !(beginTime >= interval.getEndTime() || endTime <= interval.getBeginTime());
    //Fr Chris' clunky but hopefully clearer way:
    if( interval.getBeginTime()>=endTime ) return false; //interval begins after then end
    if( interval.getEndTime()<=beginTime  ) return false;  //interval ends before the start
    return true;
  }

  public String toString()
  { 
       return beginTime + "-" + endTime;
 }
}

Appointment

public class Appointment
{
  private TimeInterval time;

  public Appointment(TimeInterval time)
  {
    this.time = time;
  }

  public TimeInterval getTime()
  {  
      return time; 
  }

  public boolean conflictsWith(Appointment other)
  {
     // part a
  }

  public String toString()
  { 
      return time.toString(); 
  }
}

DailySchedule

import java.util.ArrayList;

public class DailySchedule
{
  // contains Appointment objects, no two Appointments overlap
  private ArrayList apptList;


  public DailySchedule()
  {  apptList = new ArrayList();  }


  public void clearConflicts(Appointment appt)
  {
    //part b here
  }


  public boolean addAppt(Appointment appt, boolean emergency)
  {
    //part c here
  }

  public String toString()
  { return apptList.toString(); }
}

AppointmentDriver to test out your results

public class AppointmentDriver
{
  public static void main(String[] args)
  {
    DailySchedule schedule = new DailySchedule();
    System.out.println("6-8 added: " +
      schedule.addAppt(new Appointment(new TimeInterval(6, 8)), false));

    System.out.println("8-9 added: " +
      schedule.addAppt(new Appointment(new TimeInterval(8, 9)), false));

    System.out.println("11-12 added: " +
      schedule.addAppt(new Appointment(new TimeInterval(11, 12)), false));

    System.out.println("12-14 added: " +
      schedule.addAppt(new Appointment(new TimeInterval(12, 14)), false));

    System.out.println("10-12 added: " +
      schedule.addAppt(new Appointment(new TimeInterval(10, 12)), false));

    System.out.println("10-12 added (emergency): " +
      schedule.addAppt(new Appointment(new TimeInterval(10, 12)), true));

    System.out.println("The Day's schedule:\n"+schedule);
  }
}