using the setSize and setLocation methods of Rectangle class
hi everyone. how are you?
i have a class square that inherits the rectangle class. what i have is center coordinates of the center of a square. i have figured the arithmetic to getting the coords of the top left coord of the square (rectangle). what i need to do is to use the methods setSize and setLocation of the Rectangle class to tell the program that these new coordinates are where the square actually is, and then i can return the values to the user, but how do i properly use the methods setSize and setLocation?
public class Square extends Rectangle
public int x;
public int y;
public int sideLength;
public Square(int xCoord, int yCoord, int lengthOfSide)
x = xCoord;
y = yCoord;
sideLength = lengthOfSide;
int xCoordTopLeft = x - sideLength / 2;
int yCoordTopLeft = y + sideLength / 2;
Rectangle r = new Rectangle(x, y);
[B]r.setLocation(xCoordTopLeft, yCoordTopLeft); // error - says parentheses are misplaced
r.setSize(sideLength, sideLength); //error[/B]
public int getArea()
int area = sideLength * sideLength;
Re: using the setSize and setLocation methods of Rectangle class
Your code must be placed inside of a method (or at least a static block). Otherwise, if it's not initializing a declared variable, Java will complain.