Results 1 to 11 of 11
  1. #1
    goliwala is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default [SOLVED] defining methods

    Hi all, I need some help. I have two methods (X_Position and Y_Position) that I am using to calculate user input on Cartesian coordinate. If I am sitting at 5, 5 and facing North then user input of 5 should give me 5, 10. If I am facing east and sitting at 5, 5 then user input of 5 should give me 10, 5. Can someone please help? Forward is user input. This is my first ever programming class so please be kind.

    public static int X_Position(int currentXPosition)
    {
    while (direction == 'E')
    {
    currentXPosition = (XPosition + forward);
    }
    while (direction == 'W')
    {
    currentXPosition = (XPosition - forward);
    }
    return currentXPosition;
    }


    public static int Y_Position(int currentYPosition)
    {
    while (direction == 'N')
    {
    currentYPosition = (YPosition + forward);
    }
    while (direction == 'S')
    {
    currentYPosition = (YPosition - forward);
    }
    return currentYPosition;
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Can you show what the methods do now and why that is wrong and what you want the output to be?

    Write a small test script that calls your methods. Set the values you describe, call the methods and use println()s to show the input , calculatiions and the output. Copy and post all that here.

  3. #3
    goliwala is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Here is the code I have. These two methods should return pacman's new position relative to x and y axis. Now I canít even get the program working. I am really a beginner. First ever programming class. Iíve been up all night and can't even think straight now. One more thing. I have to include static with all my variables, otherwise I get "non-static variable XPosition cannot be referenced from a static context." message.

    import java.util.*;
    import java.io.*;

    public class testingmethods
    {

    static int XPosition;
    static int YPosition;
    static char direction;
    static int forward;


    static Scanner console = new Scanner(System.in);
    public static void main (String[] args)
    {
    System.out.println("To create a Pacman in a certain position on the "
    + "graph, please enter x-axis, y-axis, and "
    + "direction (N, S, E, or W).");

    testingmethods p1 = new testingmethods();
    p1.XPosition = console.nextInt();
    p1.YPosition = console.nextInt();
    p1.direction = console.next().charAt(0);

    System.out.println("Your Pacman is facing " + direction + " and position "
    + "on the graph is " + XPosition + ", " + YPosition);

    System.out.println("To move the pacman, enter a number.");

    p1.forward = console.nextInt(); // getting user input to move pacman forward

    System.out.print("pacman's new position is " + X_Position(currentXPosition) + ", " + Y_Position(currentYPosition));


    }

    public static int X_Position(int currentXPosition)

    {
    while (direction == 'E')
    {
    currentXPosition = (XPosition + forward);
    }
    while (direction == 'W')
    {
    currentXPosition = (XPosition - forward);
    }
    return currentXPosition;
    }


    public static int Y_Position(int currentYPosition)

    {
    while (direction == 'N')
    {
    currentYPosition = (YPosition + forward);
    }
    while (direction == 'S')
    {
    currentYPosition = (YPosition - forward);
    }
    return currentYPosition;
    }

    }

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    I don't know why you're getting that specific error, though you should read up more on the "static" keyword and modify your code (mostly your main method) accordingly. There seems to be a lot of confusion with static and instances.

  5. #5
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    did you have any luck on your identical post on Sun.com's Java forums?

  6. #6
    goliwala is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default defining methods

    no luck on Sun.com's Java forums. They say to get rid of staic as well but I can't figure out how to do it. as soon as I remove static from any od the variables, I get the error. I got the program working to give correct output but with static variables. If someone can give me an idea on how to correct the problem so my variables are not static. In the mean time I am reading away.
    Thanks everyone for checking.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm just look at this thread now. So I've no idea what your error message is.

    Anyway,

    Java Code:
    p1.direction = console.next().charAt(0);
    This is ok, you are accessing a static variable in static contest. So the most suitable thing is use the class reference.

    Java Code:
    testingmethods.direction = console.next().charAt(0);

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In following line of code,

    Java Code:
            System.out.print("pacman's new position is " + X_Position(currentXPosition) 
                    + ", " + Y_Position(currentYPosition));
    where did you define currentXPosition and currentYPosition

  9. #9
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by goliwala View Post
    no luck on Sun.com's Java forums. They say to get rid of staic as well but I can't figure out how to do it. as soon as I remove static from any od the variables, I get the error. I got the program working to give correct output but with static variables. If someone can give me an idea on how to correct the problem so my variables are not static. In the mean time I am reading away.
    Thanks everyone for checking.
    While someone may be able to fix this particular problem for you, the real problem is that you don't yet understand what the static modifier does. A good Java OO tutorial should rectify that

  10. #10
    goliwala is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default defining methods

    Thanks Eranga. your advise has solved my problem. You guys are right, I am really new to this and having very hard time grasping methods and objects concept. Thanks again everyone.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Defining own Exception
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-12-2007, 10:09 AM
  3. defining own exception
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-12-2007, 12:47 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
  •