?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Using Nested For Loops to Add Point Objects to ArrayList

APCSA-PY6GTJ

Consider the Point class below.

public class Point 
{
    private int x;
    private int y;

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int getX()
    {
        return x;
    }

    public int getY()
    {
        return y;
    }

    public void setX(int x)
    {
        this.x = x;
    }

    public void setY(int y)
    {
        this.y = y;
    }

    public String toString()
    {
        return "(" + x + ", " + y + ")";
    }
}

The points array list is composed of unique Point objects.

Which of the following loops will populate the points array list with all combinations of points that have x-values between 1 and 3, and y-values between 1 and 5?

A
ArrayList<Point> points = new ArrayList<Point>();
for (int a=1; a<=5; a++)
{
    for (int b=1; b<=3; b++)
    {
        points.add(new Point(a,b));
    }
}
B
ArrayList<Point> points = new ArrayList<Point>();
for (int a=1; a<=5; a++)
{
    for (int b=1; b<=3; b++)
    {
        points.add(a,b);
    }
}
C
ArrayList<Point> points = new ArrayList<Point>();
for (int a=1; a<=3; a++)
{
    for (int b=1; b<=5; b++)
    {
        points.add(new Point(a,b));
    }
}
D
ArrayList<Point> points = new ArrayList<Point>();
for (int a=1; a<=3; a++)
{
    for (int b=1; b<=5; b++)
    {
        points.add(a,b);
    }
}
E
ArrayList<Point> points = new ArrayList<Point>();
for (int a=0; a<points.size(); a++)
{
    for (int b=0; b<points.get(a).size(); b++)
    {
        points.set(a, new Point(a,b));
    }
}