jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes calling another method from another class, interface issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calling another method from another class, interface issue" Watch "calling another method from another class, interface issue" New topic

calling another method from another class, interface issue

jim gotti
Ranch Hand

Joined: Jul 02, 2002
Posts: 36
i have an interface (Meth) that is implimented by a class (Rectangle).
there is a class named Point that the interface references to

Now, i want to pass Lengths of the sides of a rectangle , and get the 4 endpoints of that rectangle.
the Rectangle class is as follows :
[ fixed UBB tag syntax error and code formatting -ds ]
The body of the getEnds method, takes the LENGTHS of the lines of a rectangle and returns the endpoints. It is supposed to utilize the toString method from the Point class, but i cannot figure out how to access that toString from the getEnds method, nor can i figure out how to "convert" the endpoints from LENGTHS of the lines. Ex. new Rect(1.0,2.0) would result in the first point(0.0,0.0)Always starts at origin, the second being (1.0,0.0), third (1.0,2.0) and last (0.0,2.0). goes around the rectangle counter-clockwise.
any takers?
Thank you.
[ July 29, 2002: Message edited by: Dirk Schreckmann ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
My first question is: is that really the way you want to do this? In the hierarchy you sdescribed you are basically saying that a Rect is a Meth or behaves like a Meth. Which is fine if there is some functionality you can factor out of a Rect into a Meth. If this is a school assignment and you have to do it this way then you're stuck but it doesn't look like good OO design from what you've said.
The problem you're having is that no-where in the code do you have any Point objects. You need a Point object(s) to call the Point methods on.
I normally think of an interface as abstrating behavior while you can use a Base class (abstract or concrete) to abstract structure. For example it makes more sense to me to have a Rect object made up of Point objects, or you could have Rect object that has one Point object as its origin and then to lengths to represent its height and width.
You can do what it is you're trying to do, but in the Rect class you are going to need at least one Point object. The Rect class is going to get he methods declared in the Meth interface, now you need something in the Rect class to call those methods on.
From what you've described the Rect always has one point at (0,0) so create a point object in the Rect class in the constructor set it to (0,0). Now you can use that Point object to call the methods in the Point class. So you can get the coordinates of that point and do what you need to do with them. IF you are beng forced to use the interface to work with the Points then in the interface you would put whatever methods you're going to need. You could have a getNextPoint method that works on a Point object and takes a length and a width as arguments and returns a Point object that has the coordinates you need.

Then with that Point you can move to the next one. In the method that calls getNextPoint you'll need to determine exactly what values to put in for the arguments - I'll leave that for you.
Keep in mind there are probably quite a few ways to solve this, others will surely post their sugestions too.
Hope that helps

jim gotti
Ranch Hand

Joined: Jul 02, 2002
Posts: 36
Thank you Dave for taking the time to read over my post. Yes , unfortunately i am stuck using this code for an assignment. I originally just changed the instance vars in Point to public, but that was my way of "fenagling" the problem heh.
I just talked to a friend and he said that i have to use accessor methods, just like you posted. (i.e. using get to "lift" bypass the private constraint on the vars.
it didnt hit me until about an hour ago that the starting point is constant, which makes it so much simpler to work with. And the point objects are to be created with a main test method.
Again, i appreciate the insight, now back to getting this done the 'right' way, not my fenagled way =).
I agree. Here's the link: http://aspose.com/file-tools
subject: calling another method from another class, interface issue
It's not a secret anymore!