Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Point class and methods  RSS feed

 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a method for a second class that simply computes the distance of two point coordinates. I have already coded constrctors, accessors, and mutators, but I am struggling with programming using a this."" statement. I feel like I have the correct algorithm to find the distance, but I am not able to get the output from it.

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Sak wrote:I feel like I have the correct algorithm to find the distance, but I am not able to get the output from it.

Several things:
1. Your distance() method appears to try to calculate the distance between two 2-D points, but your Point class contains three coordinates.
2. You also cache the distance value when you calculate it, which doesn't make any sense since a point can't "have" a distance (except possibly from 0,0,0).
3. There's no point in caching it anyway, since you've made the class mutable - so you'd have to recalculate every time a coordinate changes.
4. double distance = Math.sqrt(PointX) * (PointX) + ... won't compile because you cant multiply (or add) Points.

HIH

Winston
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your use of variable names beginning with an upper case letter makes your code very unreadable, especially when you have a class name of Point and a variable of PointX.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:4. double distance = Math.sqrt(PointX) * (PointX) + ... won't compile because you cant multiply (or add) Points.

Exactly! PointX is a double not a Point. Poor naming style.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Formula for distance between two Point's:

If you think this gives too much away delete it.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code won't compile.

distance() is not a static method so you can't call it as Point.distance(...).
Your distance() takes two doubles and you are passing in two Point objects.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Exactly! PointX is a double not a Point. Poor naming style.

Ooops. My mistake (well spotted).

However, it's still wrong because the only thing that will be √-ed is the first argument.

@Jeff: My advice: Slow down, and think about what you're doing. If you don't know the formula for the distance between two points (two- or three-D), look it up (or ask someone). And then think carefully about how you should fit it into your class ... before you write any code.

In general, your indenting and spacing is good, but try to stick to conventions - specifically: ALL method and variable names should start with a lower case letter.

HIH

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