?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Define Constructor Method in Golf Bag Class

APCSA-ETLYYY

Given the following class:

public class GolfBag 
{
    private ArrayList<Equipment> equipment;  // equipment in bag
    private String golferName;               // golfer who owns bag
    private boolean color = false;           // is golf bag colorful (not BLACK or WHITE)?

    // other data and methods not shown
}

Define the constructor method that accepts as parameters the golfer’s name, the equipment in the bag, and the name of the primary color of the bag. Populate the instance variable named color as true or false. The col parameter will be in all uppercase letters. If the color is WHITE or BLACK then the color instance variable should be false, otherwise true.

A
public GolfBag(String name, ArrayList<Equipment> equip, String col)
{
    golferName = name;
    equipment = equip;
    if (col.equals("BLACK") || col.equals("WHITE"))
        color=!color;
}
B
public GolfBag(String name, ArrayList<Equipment> equip, String col)
{
    golferName = name;
    equipment = equip;
    if (!(col.equals("BLACK") && col.equals("WHITE")))
        color=false;
    else
        color=true;
}
C
public GolfBag(String name, ArrayList<Equipment> equip, String col)
{
    golferName = name;
    equipment = equip;
    if (col.equals("BLACK") || col.equals("WHITE"))
        color=false;
    else
        color=true;
}
D
public GolfBag(String name, ArrayList<Equipment> equip, String col)
{
    golferName = name;
    equipment = equip;
    color = col;
}
E
public GolfBag(String name, ArrayList<Equipment> equip, String col)
{
    golferName = name;
    equipment = equip;
    if (!(col.equals("BLACK")) || !(col.equals("WHITE")))
        color=false;
    else
        color=true;
}