?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

ArrayList of Baby Objects: Adjusting the Weights of Babies

APCSA-QTXZYK

Consider the Baby class.

public class Baby 
{
    private String firstName;
    private String lastName;
    private double weight;  // in pounds

    public Baby(String firstName, String lastName, double weight)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.weight = weight;
    }

    public double getWeight()
    {
        return weight;
    }

    public void setWeight(double weight)
    {
        this.weight = weight;
    }
    // other methods not shown
}

It was noticed that the baby scale at the hospital was not calibrated correctly one morning. If the baby's recorded birth weight was over 6.5, their true weight is really 10% lower than the recorded weight. Otherwise, the recorded weight is accurate.

Which code segment will loop through this array list of Baby objects and adjust the weights accordingly?

ArrayList< Baby > babies = // a list full of Baby objects
A
for (int i=0; i<babies.size(); i++)
{
    if (babies.getWeight() > 6.5)
        babies.setWeight(babies.getWeight()*.9);
}
B
for (int i=0; i<babies.size(); i++)
{
    if (babies.get(i).weight > 6.5)
        babies.get(i).weight = babies.get(i).weight*.9);
}
C
for (int i=0; i<babies.size(); i++)
{
    if (babies.get(i).getWeight() > 6.5)
        babies.get(i).setWeight(babies.get(i).getWeight()*.9);
}
D
for (int i=0; i<babies.size(); i++)
{
    if (babies.get(i).getWeight() <= 6.5)
        return;
    babies.get(i).setWeight(babies.get(i).getWeight()*.9);
}
E
for (int i=0; i<babies.size(); i++)
{
    if (babies.get(i).getWeight()>6.5)
        babies.get(i).setWeight(babies.get(i).getWeight()*1.1);
}