?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Removing Elements from an ArrayList of Objects

APCSA-4EVGKW

Given 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 drawing array list is composed of unique Point objects that define a drawing. What is the output after the following code segment completes execution?

ArrayList<Point> drawing = new ArrayList<Point>();
drawing.add(new Point(1,20));
drawing.add(new Point(1,25));
drawing.add(new Point(1,30));
drawing.add(new Point(99,20));
drawing.add(new Point(10,10));
drawing.add(new Point(10,20));
drawing.add(new Point(20,30));
drawing.add(new Point(10,99));

removePoint(drawing, 10, 20);
System.out.println(drawing);

public static void removePoint(ArrayList<Point> drawing, int x, int y)
{
    for (int i=0; i<drawing.size(); i++)
    {
        if (x==drawing.get(i).getX() || y==drawing.get(i).getY())
        {
            drawing.remove(i);
            if (x==drawing.get(i).getX() && y==drawing.get(i).getY())
                return;
            i--;
        }
    }
}
A
[(1, 25), (1, 30), (10, 20), (20, 30), (10, 99)]
B
[(1, 25), (1, 30), (20, 30)]
C
[(1, 25), (1, 30)]
D
[(1, 25), (1, 30), (10, 20)]
E
[(1, 25), (1, 30), (10, 20), (20, 30)]