theMobDog
theMobDog

Reputation: 1399

Which data type should I use?

I have a set of data, lets call them shapes.

There are 3 types of shapes:

1) circle :
x-coordinate
y-coordinate
r-radius
c-color

2) square:
x-coordinate
y-coordinate
s-side
c- color

3) line:
x-coordinate
y-coordinate
x1-coordinate
y1-coordinate
c- color

Which data type is best suited for them? Should I make a shape class and make the circle, square and line as subclasses of shape? If I do, can I put all of them in one class file?

As a side note: When creating each shape, constructor will be given only maxX and maxY. The shaped will be autogenerated with random numbers which will stay inbound within (0,0,maxX,maxY). Their sizes are also user-determined. I am keeping them from 1/10 to 1/3 or the width of screen. But they are less-important details.

Upvotes: 1

Views: 207

Answers (5)

theMobDog
theMobDog

Reputation: 1399

Let me first state that this is not the best solution but I ended up using factory methods for different shapes. Since we only have 3 shapes and being a relatively small program. Making a class for each one, seems like killing an ant with a sledgehammer.

I learnt a lot from all the different answers and opinions. You guys rocks and keep rocking on!

Thanks, theMobDog

Upvotes: 0

Zubair Nabi
Zubair Nabi

Reputation: 1056

Use Long for the numbers. As double and float cant be used if you need precise values.

Upvotes: 0

user1196549
user1196549

Reputation:

My two cents:

An alternative representation is by giving the bounding box (X0, Y0, X1, Y1) or (X, Y, W, H), whatever the shape type.

For line segments, there can be a swap (Y0 <=> Y1) unfortunately. It may be worth to distinguish upline and downline (internally).

For circles and squares, you'll have to enforce the constraint X1-X0=Y1-Y0 or W=H.

This scheme is also convenient for axis-aligned rectangles and ellipses.

Whether you use a single class with a type field or virtualization is mostly a matter of taste.

Upvotes: 1

flowit
flowit

Reputation: 1442

In Java more or less everything is a class. It's not different in other datastructures you probably use, like a basic Set and more fine granular types like Treeset and Hashset. All of them are classes.

In your case you probably want an abstract class or interface to implement the general Shape. Circles, Squares, Lines would be implemented as "normal" classes which inherit from Shape.

It is a convention to use one source code file per class. Especially when the source code gets bigger and you write a lot of different methods for the different shapes this is much more readable.

But I don't get why you give your constructor the max values. Don't forget: A constructor creates one object, so you schould give it only the actual values for x and y and calculate those outside of your class.

Upvotes: 0

npinti
npinti

Reputation: 52205

You could have a super class called Shape, which would have all the common properties (x-coordinate, y-coordinate and colour).

You would then extend this class with your Circle, Square and Line classes, wherein you fill in the extra properties that each distinct item has. It is recommended that you have a separate class file for each class.

This approach would allow you to, for instance, create a List<Shape> should you need to create a collection of shapes.

Upvotes: 5

Related Questions