AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Remove from Golf Equipment ArrayList

APCSA-KQ4KNW

The golfers on your golf team carry their own bags. Your coach has asked each player to minimize the equipment in their golf bags since it will be very hot during the match. If the equipment is not required and it weighs at least 0.8 pounds, then it should be removed from their golf bag.

Consider the following Equipment and GolfBag classes.

public class Equipment 
{
    private String type;
    private boolean required;
    private double weight;  // in pounds

    public Equipment(String type, boolean required, double weight)
    {
        this.type = type;
        this.required = required;
        this.weight = weight;
    }

    public boolean getRequired()
    {
        return required;
    }

    public double getWeight()
    {
        return weight;
    }
    // other methods not shown
}

public class GolfBag 
{
    private ArrayList<Equipment> equipment;
    private String golferName;

    public GolfBag(String name, ArrayList<Equipment> equip)
    {
        golferName = name;
        equipment = equip;
    }

    public ArrayList<Equipment> getEquipment()
    {
        return equipment;
    }
    // other methods not shown
}

Which segment of code will perform the function mentioned at the beginning of this problem?

A
ArrayList<GolfBag> team = // a list of GolfBag objects, each with their own Equipment

for (int i=0; i<team.size(); i++)
{
    ArrayList<Equipment> g = team.get(i).getEquipment();
    for (int e=0; e<g.size(); e++)
    {
        if (!g.get(i).getRequired() && g.get(i).getWeight() >=.8)
        {
            g.remove(i);
            i--;
        }
    }
}
B
ArrayList<GolfBag> team = // a list of GolfBag objects, each with their own Equipment

for (GolfBag bag : team)
{
    ArrayList<Equipment> g = bag.getEquipment();
    for (Equipment equip : g)
    {
        if (equip.getRequired()==false && equip.getWeight() >=.8)
        {
            g.remove(equip);
        }
    }
}
C
ArrayList<GolfBag> team = // a list of GolfBag objects, each with their own Equipment

for (int i=0; i<team.size(); i++)
{
    ArrayList<Equipment> g = team.get(i).getEquipment();
    for (int e=0; e<g.size(); e++)
    {
        if (g.get(e).getRequired()==false && g.get(e).getWeight() >=.8)
        {
            g.remove(e);
        }
    }
}
D
ArrayList<GolfBag> team = // a list of GolfBag objects, each with their own Equipment

for (int i=0; i<team.size(); i++)
{
    ArrayList<Equipment> g = team.getEquipment();
    for (int e=0; e<g.size(); e++)
    {
        if (!g.getRequired() && g.getWeight() >=.8)
        {
            g.remove(e);
        }
    }
}
E
ArrayList<GolfBag> team = // a list of GolfBag objects, each with their own Equipment

for (int i=0; i<team.size(); i++)
{
    ArrayList<Equipment> g = team.get(i).getEquipment();
    for (int e=0; e<g.size(); e++)
    {
        if (!g.get(e).getRequired() && g.get(e).getWeight() >=.8)
        {
            g.remove(e);
            e--;
        }
    }
}