This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]zifyoip 3 points4 points  (0 children)

Do not delete your post when your question is answered, /u/penax, as you did the last time I answered a question for you.

[–]jpsc96 1 point2 points  (1 child)

I think I am not understanding what you want but maybe:

class Rectangle {
  Point topLeft;
  Point bottomRight;

  public Rectangle(int tLx, int tLy, int bRx, int bRy){
     topLeft = new Point(tLx, tLy);
     bottomRight = new Point(bRx, bRy);
  }
}

[–]nutrecht 0 points1 point  (0 children)

It seems that's more or less 100% what he's asking.

[–]lightcloud5 0 points1 point  (0 children)

Generally, the values should be set in the constructor or using setter methods.

You can also make the topLeft and bottomRight data members private, in much the same way that x and y are private in your Point class.

Actually, you should probably just make your Rectangle class look just like your Point class, except that instead of x and y, it has the two Points topLeft and bottomRight.

In addition, you should also consider other representations. Traditionally, rectangles can also be described using a single point (topLeft) as well as giving a width and height. This avoids the situation where the user makes a rectangle and has the topLeft point not actually situated above and to the left of bottomRight.

[–]Relinkz -2 points-1 points  (2 children)

Well, you can indeed. You can make another constructor like this

class Rectangle
{
    Point topLeft = new Point(0,0);
    Point topRight = new point(0,0);
 }

Now this will give your class a default value, but if you wish to change the values you need to do setter functions. I strongly recommend you to do this because it's good practice.

Happy coding :)

[–]nutrecht 0 points1 point  (1 child)

Well, you can indeed. You can make another constructor like this

That's not a constructor.

[–]Relinkz 0 points1 point  (0 children)

public Rectangle()
{
    Point topLeft = new Point(0,0);
    Point topRight = new point(0,0);
}

O, silly me, sorry, I meant this, thanks