?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Manipulating an ArrayList of Point Objects

APCSA-VTGTKX

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. The following method is intended to shift the drawing by adjusting the x and/or y pixel values of each Point object by a specific amount. The drawing must fit into a rectangle region defined by the xBorder and yBorder parameters. If adjusting the x or y values results in putting the point on or outside the border, then the point should be deleted from the array list. The following method performs this task, but you need to fill in the three blanks for //1, //2, and //3

/**
 * @param drawing = array list of Points
 * @param xIncrement = how much to shift the x-value 
 * @param yIncrement = how much to shift the y-value 
 * @param xBorder = the border for the x-value.  A Point's x-value must be <= this value
 * @param yBorder = the border for the y-value.  A Point's y-value must be <= this value
 */
public static void shiftDrawing(ArrayList<Point> drawing, int xIncrement, int yIncrement, int xBorder, int yBorder)
{
    for (int i=0; i<drawing.size(); i++)
    {
        int x = drawing.get(i).getX();
        int y = drawing.get(i).getY();

        //1 if statement
        {    
            drawing.remove(i);
            i--;
        }
        else
        {
            //2  update x-value of Point
            //3  update y-value of Point
        }
    }
}
A
if (xBorder > (x+xIncrement) || yBorder > (y+yIncrement))  //1  
drawing.get(i).setX(x+xIncrement);  //2
drawing.get(i).setY(y+yIncrement);  //3
B
if (xBorder <= (x+xIncrement) || yBorder <= (y+yIncrement))  //1  
drawing.get(i).setX(x+xIncrement);  //2 
drawing.get(i).setY(y+yIncrement);  //3
C
if (xBorder <= (x+xIncrement) || yBorder <= (y+yIncrement))  //1 
drawing.setX(x+xIncrement);  //2 
drawing.setY(y+yIncrement);  //3
D
if (xBorder <= (x+xIncrement) && yBorder <=(y+yIncrement))  //1  
drawing.get(i).setX(x+xIncrement);  //2 
drawing.get(i).setY(y+yIncrement);  //3
E
if (xBorder > (x+xIncrement) && yBorder > (y+yIncrement))  //1  
drawing.setX(x+xIncrement);  //2 
drawing.setY(y+yIncrement);  //3