Results 1 to 6 of 6
Thread: Basic method creation
- 02-24-2013, 02:28 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 19
- Rep Power
- 0
Basic method creation
Hi, I have a basic interface with an oval attached to a Point. I want my oval to move around the interface and change direction when it hits a border.
I'm trying to make a method that checks the points location but I don't know how to attach my method to the class Point from java.awt
The method that I made looks like this:
public String Border(Point point){
if(point.y+50>450) {return "south";}
if(point.y-10<1) {return "north";}
if(point.x+50>450) {return "east";}
if(point.x-10<1) {return "west";}
else return "center";
}
and I want to use it like this:
if(point.Border=="south") {point.GoNorthEast();} //I didn't make method for GoNorthEast yet, I wanna know how to use my Border method first.
- 02-24-2013, 03:30 PM #2
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: Basic method creation
First: always use .equals(String s) method to compare strings and not == operator.
Next: you have created your Border method, just use it with exact signature
And keep convection when naming methods, they should start with lower letter..Java Code:if(point.Border(somePoint).equals("south")) { point.GoNorthEast(); }
- 02-24-2013, 03:40 PM #3
Re: Basic method creation
You didn't bother to reply to the response on your earlier thread: Editing a text file
Why should we expect any better behavior this time round?
Also, please go through the following links and edit your post accordingly:
Guide For New Members
BB Code List - Java Programming Forum - Learn Java Programming
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-26-2013, 08:32 AM #4
Member
- Join Date
- Feb 2013
- Posts
- 19
- Rep Power
- 0
Re: Basic method creation
Thanks milovan for the tips, but my problem still stands, I don't know how to add the method to a class that is in java.api, so I can use it for that class.
DarrylBurke, sorry for not replying there. I read that post and understood what I had to do, but didn't had time to test it then and later on I assume repling would have just been grave post digging.
- 02-26-2013, 10:00 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Basic method creation
Write your own class (MovingPoint?) and give it a Point attribute.
Your class will have the above method.Please do not ask for code as refusal often offends.
- 02-26-2013, 08:32 PM #6
Member
- Join Date
- Feb 2013
- Posts
- 19
- Rep Power
- 0
Re: Basic method creation
I already tried that yesterday and didn't work (because I didn't knew I had to use super() to get Point's constructor)
Worked now, thanks everyone.
Another problem, seems my method isn't what I had in mind. I want to use my method like this:
However, my method has Point as paramenter, (and it seems weird to type point.border(point) when calling the method). How can I make my method so it doesn't require a parameter? (if I just declare it "Public String border()" I cannot use point.x and point.y in it.Java Code:if("south".equals(point.border())) {//do stuff}
Similar Threads
-
dynamic creation of datasources in weblogic, remote creation of jndi's through java?
By prasanth raju in forum JDBCReplies: 0Last Post: 05-18-2012, 01:29 PM -
Basic Circular Linked List - addFirst() method works improperly
By carlodelmundo in forum New To JavaReplies: 9Last Post: 11-04-2011, 03:09 AM -
A basic method with an unexpected output
By JONCOM in forum New To JavaReplies: 6Last Post: 02-01-2011, 07:25 AM -
Accessing instance outside of creation method
By meringue in forum New To JavaReplies: 6Last Post: 03-22-2009, 01:36 AM -
Understand my logic errors and better understanding method and class creation
By freethinker89 in forum New To JavaReplies: 3Last Post: 10-06-2008, 11:03 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks