?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

ArrayList of Objects: Loading Golf Bags

APCSA-9XNAEL

Consider the following Equipment and GolfBag classes.

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

    public Equipment(String type, boolean required, double weight)
    {
        this.type = type;
        this.required = required;
        this.weight = 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;
    }

    // other methods not shown
}

Which of the segments of code will create an ArrayList of two GolfBag objects, each which contains a putter, 5 iron, and driver as its equipment?

A
ArrayList bags;
ArrayList equipment ;
equipment.add(new Equipment("driver", true, 1));
equipment.add(new Equipment("5 iron", true, 1));
equipment.add(new Equipment("putter", true, 1));
bags.add(new GolfBag("Phil", equipment));
bags.add(new GolfBag("Sergio", equipment));
B
ArrayList bags = new ArrayList();
ArrayList equipment = new ArrayList();
equipment.add(new Equipment("driver", true, 1));
equipment.add(new Equipment("5 iron", true, 1));
equipment.add(new Equipment("putter", true, 1));
bags.add(new GolfBag("Phil", equipment));
equipment = new ArrayList();
equipment.add(new Equipment("driver", true, 1));
equipment.add(new Equipment("5 iron", true, 1));
equipment.add(new Equipment("putter", true, 1));
bags.add(new GolfBag("Sergio", equipment));
C
ArrayList bags = new ArrayList();
ArrayList equipment = new ArrayList();
bags.add(new GolfBag("Phil", new Equipment("driver", true, 1),
                             new Equipment("5 iron", true, 1), 
                             new Equipment("putter", true, 1));
bags.add(new GolfBag("Sergio", new Equipment("driver", true, 1),
                               new Equipment("5 iron", true, 1), 
                               new Equipment("putter", true, 1));
D
ArrayList bags = new ArrayList();
ArrayList equipment = new ArrayList();
equipment.add(new Equipment("driver", true, 1));
equipment.add(new Equipment("5 iron", true, 1));
equipment.add(new Equipment("putter", true, 1));
bags.add(new GolfBag("Phil", equipment));
equipment.add(new Equipment("driver", true, 1));
equipment.add(new Equipment("5 iron", true, 1));
equipment.add(new Equipment("putter", true, 1));
bags.add(new GolfBag("Sergio", equipment));
E
ArrayList bags = new ArrayList();
ArrayList equipment = new ArrayList();
equipment.add("driver", true, 1);
equipment.add("5 iron", true, 1);
equipment.add("putter", true, 1);
bags.add("Phil", equipment);
bags.add("Sergio", equipment);