?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Modify Luggage Costs in ArrayList of Luggage Objects

APCSA-6GFTW9

Consider the following classes.

// This class defines a flight, which contains lots of luggage
public class Flight 
{
    private String flightNum;
    private ArrayList<Luggage> luggage;

    public Flight(String flight, ArrayList<Luggage> lug)
    {
        flightNum = flight;
        luggage = lug;
    }

    public ArrayList<Luggage> getLuggage()
    {
        return luggage;
    }

    public void updateLuggageCosts(String fName, String lName)
    {
        // to be defined below
    }

    // other data and methods not shown
}

// This class defines a piece of luggage
public class Luggage 
{
    private String firstName;  // passenger who owns luggage
    private String lastName;  // passenger who owns luggage
    private double weight;     // weight of luggage in lbs
    private double cost=0;     // cost to transport this luggage

    public Luggage(String fName, String lName, double wt)
    {
        firstName = fName;
        lastName = lName;
        weight = wt;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public double getWeight()
    {
        return weight;
    }

    public double getCost()
    {
        return cost;
    }

    public void setCost(double charge)
    {
        cost = charge;
    }

    // other data and methods not shown
}

If the piece of luggage weighs over 50 pounds then an additional charge of \$25 needs to be added to the cost of transporting the luggage.

Which of the following is the BEST way to define a method in the Flight class to update the costs of transporting luggage for a specific passenger on the flight?

A
public void updateLuggageCosts(String fName, String lName)
{
    for (int i=0; i<luggage.size(); i++)
    {
        if (luggage.get(i).getWeight()>50)
        {
            luggage.get(i).setCost(25);
        }
    }
}
B
public void updateLuggageCosts(String fName, String lName)
{
    for (int i=0; i<luggage.size(); i++)
    {
        if (luggage.getFirstName().equals(fName) &&
            luggage.getLastName().equals(lName) &&
            luggage.getWeight()>50)
        {
            luggage.setCost(25);
        }
    }
}
C
public void updateLuggageCosts(String fName, String lName)
{
    for (int i=0; i<luggage.size(); i++)
    {
        if (luggage.get(i).getFirstName().equals(fName) &&
            luggage.get(i).getLastName().equals(lName) &&
            luggage.get(i).getWeight()>50)
        {
            luggage.get(i).setCost(luggage.get(i).getCost()+25);
        }
    }
}
D
public void updateLuggageCosts(String fName, String lName)
{
    for (int i=0; i<luggage.size(); i++)
    {
        if (luggage.get(i).getFirstName().equals(fName) &&
            luggage.get(i).getLastName().equals(lName) &&
            luggage.get(i).getWeight()>50)
        {
            luggage.get(i).setCost(25);
        }
    }
}
E
public void updateLuggageCosts(String fName, String lName)
{
    for (int i=0; i<luggage.size(); i++)
    {
        if (luggage.getFirstName().equals(fName) &&
            luggage.getLastName().equals(lName) &&
            luggage.getWeight()>50)
        {
            luggage.setCost(luggage.getCost()+25);
        }
    }
}