0

I am attempting to modify the point values using my MoveUp method. However I am having trouble instantiating the class and its new values in the main method. I declared Point x1 = new Point(); then x1.moveUp();, but moveUp(); creates an error that the method is not applicable for the arguments.

 public class Point { private int xcoord = 6; private int ycoord; public static void main(String[] args) { Point x1 = new Point(); x1.moveUp(); System.out.print(x1); } public Point () { xcoord = 0; ycoord = 0; } public Point (int x, int y) { x = 9; y = 8; } public int getX () { return xcoord; } public int getY () { return ycoord; } public void moveUp(int amount) { amount = xcoord + 1; } public void moveDown(int amount) { amount = ycoord - 2; } public void moveRight(int amount) { amount = xcoord + 1; } public void moveLeft(int amount) { amount = xcoord - 1; } } 
3
  • 1
    x1.moveUp();, method decleration: void moveUp(int amount) Commented Oct 5, 2016 at 6:57
  • it has a parameter of type int so pass an int argument..it will work fine Commented Oct 5, 2016 at 7:00
  • 3
    First of all, your method moveUp does nothing but modifies the argument. The newly assigned value will never be used.Second: your method has an argument, but you call it without an argument. Commented Oct 5, 2016 at 7:00

7 Answers 7

1

Your move* methods have incorrect logic.

If you wish to pass the amount by which to move the Point, as implied by the parameter name, the logic should be updating xcoord or ycoord, not the local variable amount.

For example, in order to move up you should add amount to ycoord :

 public void moveUp(int amount) { ycoord += amount; } 

Then you would call the method with :

x1.moveUp(5); 
Sign up to request clarification or add additional context in comments.

Comments

0

As you can see from the method declaration

public void moveUp(int amount) 

it requires a int parameter, so your code should be something like

x1.moveUp (50); // or some other input from your program 

Also fix the logic error in you move* methods

Comments

0

According to your code, moveUp method takes 'amount' as an argument. But you are calling it as

x1.moveUp();

i.e. without argument. That's the reason you are getting error.

Comments

0
 public void moveUp(int amount) { amount = xcoord + 1; } 

As per your method implementation your method call must be

MoveUp(someIntegerValue)

Comments

0

The suggestions about the moveUp method expecting an int as an argument are all valid - but I have noticed also that what you do inside these methods does not make sense - for instance you assign the new value into the amount which you passed in as an argument. I think what you want to do is to update the xcoord or the ycoord

With that, your code should look like this:

 public void moveUp(int amount) { xcoord += (amount+ 1); } public void moveDown(int amount) { ycoord -=(amount +2); } public void moveRight(int amount) { xcoord += (amount + 1); } public void moveLeft(int amount) { xcoord - =(amount+1); } 

And as already suggested, you call the moveUp method like this from your main:

 public static void main(String[] args) { Point x1 = new Point(); int someAmount = 50; x1.moveUp(someAmount ); System.out.print(x1); } 

I hope this helps...

Comments

0

These methods are declared wrongly!

 public void moveUp(int amount) { amount = xcoord + 1; } public void moveDown(int amount) { amount = ycoord - 2; } public void moveRight(int amount) { amount = xcoord + 1; } public void moveLeft(int amount) { amount = xcoord - 1; } 

These methods each has an amount parameter, I suppose you want the point to move up, down, left, right for a certain amount. To do that, you shouldn't assign xcoord - 1 to amount because it does nothing practical. Also, you seem to mess up the x and y coordinates.

Instead, do this:

 public void moveUp(int amount) { ycoord += amount; } public void moveDown(int amount) { ycoord -= amount; } public void moveRight(int amount) { xcoord += amount; } public void moveLeft(int amount) { xcoord -= amount; } 

Now the methods are all correct, let's see how you can call a method properly.

As I said earlier, the moveXXX methods need an amount as an argument. So if you call it like this:

x1.moveUp(); 

it doesn't work because moveUp doesn't know how much it should move up. You need to give it a value. Let's say 10:

x1.moveUp(10); 

Now x1.getX() will return 10!

Also, I suggest you overriding toString. This way, println() can actually print something meaningful:

@Override public String toString() { return "(" + getX() + ", " + getY() + ")" } 

Comments

0

public void moveUp(int amount) { amount = xcoord + 1; } remember this method is asking an int value as its argument value. So you need to give a particular int value. like : x1.moveUp(500);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.