Results 1 to 11 of 11
Thread: [SOLVED] defining methods
- 10-23-2008, 02:04 PM #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;
}
- 10-23-2008, 02:18 PM #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.
- 10-23-2008, 03:44 PM #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;
}
}
- 10-23-2008, 08:31 PM #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.
- 10-23-2008, 08:39 PM #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?
- 10-23-2008, 11:24 PM #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.
- 10-24-2008, 08:22 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I'm just look at this thread now. So I've no idea what your error message is.
Anyway,
This is ok, you are accessing a static variable in static contest. So the most suitable thing is use the class reference.Java Code:p1.direction = console.next().charAt(0);
Java Code:testingmethods.direction = console.next().charAt(0);
- 10-24-2008, 08:24 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
In following line of code,
where did you define currentXPosition and currentYPositionJava Code:System.out.print("pacman's new position is " + X_Position(currentXPosition) + ", " + Y_Position(currentYPosition));
- 10-24-2008, 08:44 AM #9
Senior Member
- Join Date
- Sep 2008
- Posts
- 135
- Rep Power
- 0
- 10-24-2008, 12:27 PM #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.
- 10-24-2008, 03:00 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Nice to see that you have solve your question, it's time to mark your thread as solved.
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
Defining own Exception
By Java Tip in forum Java TipReplies: 0Last Post: 11-12-2007, 10:09 AM -
defining own exception
By javaplus in forum New To JavaReplies: 2Last Post: 11-12-2007, 12:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks