1. Member
Join Date
Oct 2008
Posts
4
Rep Power
0

## [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. 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. Member
Join Date
Oct 2008
Posts
4
Rep Power
0
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. Senior Member
Join Date
Sep 2008
Posts
564
Rep Power
5
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. Senior Member
Join Date
Sep 2008
Posts
135
Rep Power
0
did you have any luck on your identical post on Sun.com's Java forums?

6. Member
Join Date
Oct 2008
Posts
4
Rep Power
0

## 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. 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. 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. Senior Member
Join Date
Sep 2008
Posts
135
Rep Power
0
Originally Posted by goliwala
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. Member
Join Date
Oct 2008
Posts
4
Rep Power
0

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

#### Posting Permissions

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