File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Adding methods to a point class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding methods to a point class" Watch "Adding methods to a point class" New topic
Author

Adding methods to a point class

P Derlyuk
Ranch Hand

Joined: Feb 17, 2013
Posts: 33
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

Joined: Jan 10, 2002
Posts: 61654
    
  67

Where's your try at the code? What specific problems are you having with it?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
P Derlyuk
Ranch Hand

Joined: Feb 17, 2013
Posts: 33
Honestly, I'm not even sure where to begin.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61654
    
  67

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

Joined: Feb 17, 2013
Posts: 33
Ok, i tried this and am getting a StackOverflowError.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Feb 17, 2013
Posts: 33
I figured it out. I over complicated it at first. here is what i think it should be.

 
 
subject: Adding methods to a point class