Results 1 to 5 of 5
  1. #1
    Abder-Rahman is offline Member
    Join Date
    Oct 2008
    Posts
    32
    Rep Power
    0

    Default The values are NOT as expected...

    Hello,

    I created two classes "Point" and "CreatePoint", as follows:

    1- Point.java:

    public class Point {

    public Point(int x, int y)
    {
    setXCoordinate(x);
    setYCoordinate(y);
    }

    private int x;
    private int y;

    int newX;
    int newY;

    public int getXCoordinate()
    {
    return newX;
    }

    public int getYCoordinate()
    {
    return newY;
    }

    public void setXCoordinate(int x)
    {
    int newX = x;
    }

    public void setYCoordinate(int y)
    {
    int newY = y;
    }

    }

    2- CreatePoint.java:


    public class CreatePoint
    {

    public static void main(String[] args)
    {

    Point p1, p2, p3;


    p1 = new Point(0,0);
    p2 = new Point(0,0);
    p3 = new Point(10,10);

    System.out.println("p1 --> " "(" p1.getXCoordinate() "," p1.getYCoordinate() ")" );
    System.out.println("p2 -->" "(" p2.getXCoordinate() "," p2.getYCoordinate() ")" );
    System.out.println("p3 -->" "(" p3.getXCoordinate() "," p3.getYCoordinate() + ")" );
    }
    }


    3- Expected output:

    p1 --> (0,0)
    p2 --> (0,0)
    p3 --> (10,10)

    4- Actual Output:

    p1 --> (0,0)
    p2 -->(0,0)
    p3 --> (0,0)

    WHY AM I GETTING:

    p3 --> (10,10)?

    Thanks.

  2. #2
    Abder-Rahman is offline Member
    Join Date
    Oct 2008
    Posts
    32
    Rep Power
    0

    Default

    It worked when I changed "Point.java" as follows:

    public class Point {

    private int x;
    private int y;


    public int getXCoordinate()
    {
    return x;
    }

    public int getYCoordinate()
    {
    return y;
    }

    public void setXCoordinate(int newX)
    {
    x = newX;
    }

    public void setYCoordinate(int newY)
    {
    y = newY;
    }

    public Point(int x, int y)
    {
    setXCoordinate(x);
    setYCoordinate(y);
    }

    }

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    public void setXCoordinate(int x)
    {
        int newX = x;
    }
    That creates a local variable "newX" and sets it's value (which goes out of scope as soon as the method finishes), but the instance variable "newX" remains untouched.

    you need to remove one word from that definition (and from the other setter definition) and it will work. I will let you try to determine which it is.

    Edit: Too slow. Do you understand why it worked, though?

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

    Default

    this compiles? your print statements look like they'd stop right there

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Your right emcee... this shouldn't compile unless + signs are added into the print statements. To the OP: Please use code tags ([ code ][ /code ] without the spaces) to indicate code. Code blocks keep spacing and tabs and makes code easier to read.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  2. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  3. Error: ')' expected
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:32 AM
  4. Error: '{' expected
    By romina in forum New To Java
    Replies: 1
    Last Post: 07-26-2007, 09:34 AM
  5. MSG ERROR: : expected
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-31-2007, 02:21 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
  •