Hi all!
I have moved on to learning about classes.....they don't seem as bad as the past things I have done so far. I have, however, hit a few snags along the way.
Here is my code for my Rectangle class
public class Rectangle
{
private int x;
private int y;
private int height;
private int width;
public Rectangle(int initialX, int initialY, int initialWidth, int initialHeight)
{
x = initialX;
y = initialY;
width = initialWidth;
height = initialHeight;
}
public int getHeight()
{
return height;
}
public int getWidth()
{
return width;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public boolean equals(Rectangle o)
{
return (x == o.x
&& y == o.y
&& width == o.width
&& height == o.height);
}
}
And here is the code for my client class.
public class testRectangle
{
public static void main(String[] args)
{
Rectangle r1 = new Rectangle(10,10,100,100);
Rectangle r2 = new Rectangle(5,5,50,50);
System.out.println("Coordinate X is: (" + r1.getX() + ")");
System.out.println("Coordinate Y is: (" + r1.getY() + ")");
System.out.println("The Width is: (" + r1.getWidth() + ")");
System.out.println("The Length is: (" + r1.getHeight() + ")");
if(r1.equals(r2))
System.out.println("Both Rectangles Are Equal!");
else
System.out.println("Both Rectangles Are Not Equal!");
}
}
Ok now here are my issues....I know most of you have seen this exact same method done to death BUT what is the point of the coordinates? Why are they needed and what do they do? Did I do the width and height incorrectly and the coordinates are meant to determine these 2 things? I am confused as to why the coordinates x and y are even needed. And also, is my boolean method in my rectangle class correct?
And for my way of checking if both rectangles are equal in my client...is this the correct way of doing it?
I value all and any input please.....criticize my code as harshly as you can so that I can better improve my skills in programming. positive comments are also welcome as the boost morale LOL =)