Page 1 of 1

Reputation:

# An improved Rectangle Class

Posted 28 March 2010 - 12:36 PM

Hey, all. I am attempting to solve a problem using the Square and Point classes from the API, but given my limited experience with inheritance and class instantiation, I came here to get a few opinions on the matter. First off all, I would like to create a rectangle using only four sets of points to define the space the rectangle should contain. From this I want to be able to determine length, width, area and perimeter - using only four sets of points (obviously I will need some help from Math). My first question is, would it be wise to use the Point class in a separate .java file, and build a Rectangle class in another and use the two in a main method as a test? I thought maybe storing the x,y coordinates in a Point class would be better than building them in as attributes in a single Rectangle class - this would create an obvious problem of having too many integers x1,y1,x2,y2,x3,y3,x4,y4.

Is This A Good Question/Topic? 0

## Replies To: An improved Rectangle Class

### #2 Martyr2

• Programming Theoretician

Reputation: 3911
• Posts: 11,448
• Joined: 18-April 07

## Re: An improved Rectangle Class

Posted 28 March 2010 - 02:08 PM

Well keep in mind that you really only need to store two points for your rectangle. The top left and the bottom right. Using those two points you can easily construct what the lower left and top right values are. The top right for instance is the X value from the bottom right and the Y value of the top left. So really it is up to you whether you store these values as a point or variables. If it was me, I am a sucker for making a nice class for something like point because it always lends itself to future flexibility and maintenance. For instance, add a "Z" coordinate later to the point class and suddenly you can build cubes in 3D space etc.

So if you are new, want practice, and come up with something that has some future options... a point class may be the way to go.