Hotel is #1 from the from the 2005 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/apc/_ap05_frq_comp_sci_a_45544.pdf

On the 2005 AP CS A Exam, the ArrayList remove method returned a reference of type Object. It was necessary to cast the reference to the correct type to call most methods. If this question appeared on a more recent AP CS A Exam, waitList would have been declared as ArrayList<String> waitList and casting would not have been required.

Part (a) requestRoom method

public Reservation requestRoom(String guestName)
{
    for(int i = 0; i < rooms.length; i++)
    {
        if(rooms[i] == null)
        {
            rooms[i] = new Reservation(guestName, i);
            return rooms[i];
        }
    }
    
    waitList.add(guestName);
    return null;
}

Part (b) cancelAndReassign method

public Reservation cancelAndReassign(Reservation res)
{
    int index = res.getRoomNumber();
    
    if(waitList.size() > 0)
    {
        String guestName = (String) waitList.remove(0);
        rooms[index] = new Reservation(guestName, index);
    }
    else
    {
        rooms[index] = null;
    }
    
    return rooms[index];
}

2005 AP CS Exam Free Response Solutions

Help & comments

Get help from AP CS Tutor Brandon Horn

Comment on Hotel