?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Cancel Reservation from ArrayList of Reservation Objects

APCSA-6KXVJV

Consider the following class.

public class Reservation
{
    private int id;
    private String name;
    private String flightNum;

    public Reservation(int id, String name, String flightNum)
    {
        this.id = id;
        this.name = name;
        this.flightNum = flightNum;
    }

    public int getId()
    {
        return id;
    }

    // other methods not shown
}

The reservations array list is full of Reservation objects. The cancelReservation method should take this list, delete the reservation with the given reservation id, then add that reservation to the canceled array list.

The parameters passed to this method are in this order:

1) the reservations array list
2) the reservation id
3) the cancelled reservations array list.

Which of the following methods from a separate class performs this function?

A
public static void cancelReservation(ArrayList<Reservation> r, int id, ArrayList<Reservation> c)
{
    for (int i=0; i<r.size(); i++)
    {
        if (r.get(i).getId()==id)
        {
            c.add(r.remove(id));
        }
    }
}
B
public static void cancelReservation(ArrayList<Reservation> r, int id, ArrayList<Reservation> c)
{
    for (int i=0; i<r.size(); i++)
    {
        if (r.get(i).getId()==id)
        {
            c.add(r.remove(i));
        }
    }
}
C
public static void cancelReservation(ArrayList<Reservation> r, int id, ArrayList<Reservation> c)
{
    for (int i=0; i<r.size(); i++)
    {
        if (r.get(i).getId()==id)
        {
            r.remove(i);
            c.add(r.get(i));
        }
    }
}
D
public static void cancelReservation(ArrayList<Reservation> r, int id, ArrayList<Reservation> c)
{
    for (int i=0; i<r.size(); i++)
    {
        if (r.get(i).id==id)
        {
            c.add(r.get(i));
            r.remove(i);
        }
    }
}
E
public static void cancelReservation(ArrayList<Reservation> r, int id, ArrayList<Reservation> c)
{
    for (int i=0; i<r.size(); i++)
    {
        if (r.contains(id))
        {
            c.add(r.get(i));
            r.remove(i);
        }
    }
}