Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Point class and methods

Jeff Sak
Ranch Hand
Posts: 38
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
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
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
• 1
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
Formula for distance between two Point's:

If you think this gives too much away delete it.

Carey Brown
Bartender
Posts: 2996
46

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
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

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?