2008 Trip Flights

<< 2009NumberTile | APQuestionsTrailIndex | 2008Encoding >>

Question from College Board

Trip

2008 question 1... here is everything but the solution if you want to test to see if your solution would work....

import java.util.*;

public class Trip
{

    private ArrayList<Flight> flights;

    /**
     * Constructor for objects of class Trip
     */
    public Trip()
    {
        flights = new ArrayList<Flight>();
    }


    public int getDuration()
    {
        // part a
       return -1;
    }
    public int getShortestLayover()
    {
        // part b

        return -1;
    }
    public void add(Flight f)
    {
        flights.add(f);
    }
}

Flight



public class Flight
{

    private Time dep;
    private Time arr;

    /**
     * Constructor for objects of class Flight
     */
    public Flight()
    {
        // initialise instance variables
        dep=new Time();
        arr=new Time();
    }
    public Flight(Time d, Time a )
	{
		// initialise instance variables
		dep=d;
		arr=a;
	}

    public Time getDepartureTime()
    {
        // put your code here
        return dep;
    }
    public Time getArrivalTime()
    {
        // put your code here
        return arr;
    }
}

Time


public class Time
{
	// instance variables - replace the example below with your own
	private int hr;
	private int min;

	/**
	 * Constructor for objects of class Time
	 */
	public Time()
	{
		// initialise instance variables
		hr = 0;
		min=0;
	}
    public Time (int h, int m)
    {
        hr=h;
        min=m;
    }
	public int getHour()
	{
	    return hr;
	   }
	public int getMin()
	{
	    return min;
	}
	public int minutesUntil(Time other)
	{

		return 60*(other.getHour()-hr) + other.getMin()-min;
	}
}

ExampleTripDriver


public class ExampleTripDriver
{

    public static void main(String[] args)
    {
        Trip empty= new Trip();
        Trip vacation=new Trip();
        vacation.add(new Flight(new Time(11,30), new Time(12,15)));
        vacation.add(new Flight(new Time(13,15), new Time(15,45)));
        vacation.add(new Flight(new Time(16,0), new Time(18,45)));
        vacation.add(new Flight(new Time(22,15), new Time(23,0)));
        System.out.println("Empty duration (should be 0)"+empty.getDuration());
        System.out.println("Vacation duration: (should be 690)"+vacation.getDuration());
        System.out.println("Empty Shortest Layover (should be -1): "+empty.getShortestLayover());
        System.out.println("Vacation Shortest Layover (should be 15): "+vacation.getShortestLayover());
    }


}

Solution

Part (a)

Hint: Look at the flights Arraylist. we need the begining of the first on the list, and the end time of the last on the list. There are handly methods getDepartureTime(), get ArriavalTime() and minutesUntil(). They are methods that come with the Time class

Part (b)

We need to go through the Arraylist of flights again. this time we use the minutesUntil() method from the end of one flight to the beginning of the next flight. We dont need to keep track of all these wait times (though your could), but you do need to keep track of the minimum wait time. you could compare the current wait time with the last, and change the minimum time if it is smaller.