File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding methods to a point class

 
P Derlyuk
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble getting started on this practice problem. Any help would be great!


Add the following method to the Point class:

public int manhattanDistance(Point other)

Returns the "Manhattan distance" between the current Point object and the given other Point object. The Manhattan distance refers to how far apart two places are if the person can only travel straight horizontally or vertically, as though driving on the streets of Manhattan. In our case, the Manhattan distance is the sum of the absolute values of the differences in their coordinates; in other words, the difference in x plus the difference in y between the points.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where's your try at the code? What specific problems are you having with it?
 
P Derlyuk
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Honestly, I'm not even sure where to begin.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by adding an empty method with the specified signature. Then it can start to be filled out; one step at a time.
 
P Derlyuk
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, i tried this and am getting a StackOverflowError.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P Derlyuk wrote:Ok, i tried this and am getting a StackOverflowError.


Then some method or constructor is calling itself endlessly. It's somewhere in the code you haven't shown. However, if that's a piece of your Point class, then this line:

is the cuprit.

It's called every time the enclosing class (Point, I assume) is instantiated. And it instantiates a Point, which, if this is part of the Point class, executes that line again, and so on.

Why do you have a Point object containing another Point object?
 
P Derlyuk
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out. I over complicated it at first. here is what i think it should be.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic