• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need to create a n-dimensional point with a constructor with unknown number of dimensions.  RSS feed

 
javier rosas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create a n-dimensional point with unknown number of dimensions. I have to use a sentinel value to stop making dimensions and then go to the next point and do the same. Once I have the two points I need to make a line and get the line lenght. My problem is that I dont know how to start a constructor with unknown number of dimensions. In the last project we had x,y,z as dimensions but in this project is unknown until user interaction. Any help would be appreciated.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you get the dimension from user input eg using Scanner class? That way you can get the number.

Another way is to use vararg constructor eg new Point(int... dim) = this takes in 0 to many ints
 
javier rosas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to keep asking the user for a number in point1 in a loop until user presses enter it goes to the next point and loops again once I have those two points I have to use the get line lenght formula.

Is this right
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Javier,

I'm not very clear about what you're trying to do, but that won't stop me from guessing.

It sounds like you are trying to define a point or a vector (in the mathematical sense) in n-space then create an array of these points.

If I get it right you want to use a constructor for a Point class that looks like
Point x = new Point(1,2);
Point y = new Point (1,2,3);
Point z = new Point(1,2,3,4);

If the number of dimensions has a fairly small and know maximum then I would tend to overload the constructor. If either of those conditions were not met I would use the vararg construct as K. Tsang suggested a google search on "java varargs" produces lots of tutorials.

Internally the Point class can allocate an array of the appropriate size for the coordinates.

Joe
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!