Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By milovan
  • 1 Post By Tolls

Thread: Basic method creation

  1. #1
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default 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.

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default 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
    Java Code:
    if(point.Border(somePoint).equals("south")) {
        point.GoNorthEast();
    }
    And keep convection when naming methods, they should start with lower letter..
    alexix likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default 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.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Basic method creation

    Write your own class (MovingPoint?) and give it a Point attribute.
    Your class will have the above method.
    alexix likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    alexix is offline Member
    Join Date
    Feb 2013
    Posts
    19
    Rep Power
    0

    Default Re: Basic method creation

    Quote Originally Posted by Tolls View Post
    Write your own class (MovingPoint?) and give it a Point attribute.
    Your class will have the above method.
    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:
    Java Code:
    if("south".equals(point.border())) {//do stuff}
    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.

Similar Threads

  1. Replies: 0
    Last Post: 05-18-2012, 02:29 PM
  2. Replies: 9
    Last Post: 11-04-2011, 04:09 AM
  3. A basic method with an unexpected output
    By JONCOM in forum New To Java
    Replies: 6
    Last Post: 02-01-2011, 08:25 AM
  4. Accessing instance outside of creation method
    By meringue in forum New To Java
    Replies: 6
    Last Post: 03-22-2009, 02:36 AM
  5. Replies: 3
    Last Post: 10-07-2008, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •