• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[2D Arrays] maximumDistance

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So here's my assignment question.

" double maximumDistance(double[][] pts) that is given a two-dimensional array pts that is guaranteed to have at least two rows, and each row is guaranteed to have exactly three elements. Each row of this array pts gives the (x, y, z) spatial coordinates of a point in the three-dimensional space. This method should find the two points that have the maximum distance from each other, and return this distance.

To calculate the distance between two points p1 and p2 given as double[], use the method



And here's my code for the question.



My tester has revealed there is an error
53
java.lang.ArrayIndexOutOfBoundsException: 53 at TwoDeeArrayProblems.maximumDistance(TwoDeeArrayProblems.java:98) at TestTwoDeeArrayProblems.testMaximumDistance(TestTwoDeeArrayProblems.java:97)

the line it's referring to is




I'm at a loss at what to do nd i was wondering if i can ask for some assistance. Maybe i inputted something wrong?

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'i' goes from 1 to rows-1. The pts array has rows 0 to rows-1. On line 31 you are adding 1 to the row so that would be 2 to rows. The index must be less than 'rows' so you exceed the limit.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This double[][] is very confusing. Can't you create a class MyPoint containing a double[], so that you can transform that double[][] into a one dim array of MyPoints? That would simplify the code.

You can leave out the Math.sqrt from the distance method. You only need to take the sqrt of the max distance you found (why?)
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do away with all your temp variables, pts[n] returns an array of three elements (x,y,z). So, you'd end up with
Like wise for the code in your loop.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To compare all possible combinations of pts rows you'll need to revise this loop. Note that distance(A,B) is the same as distance(B,A), so don't test the same combination of rows.
 
zack redmen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh I see what my mistake is! Thank you so much.
 
You would be much easier to understand if you took that bucket off of your head. And that goes for the tiny ad too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic