reference objects (newbie question)

Discussion in 'Java' started by Roderik, Nov 28, 2003.

  1. Roderik

    Roderik Guest

    Hi,

    I'd like to make a function to manipulate objects of a given class.

    Let's say there are two objects, called MyPoint1 and MyPoint2.
    They are both of the class cCustomPoint and contain an x an y
    attribute.

    The function should assign a value for x to a passed object (MyPoint1
    or MyPoint2).

    How should I make this function so it knows which object to manipulate
    and the value to be assigned?

    Kind regards,

    Roderik
    Roderik, Nov 28, 2003
    #1
    1. Advertising

  2. "Roderik" <> wrote in message
    news:...
    > Hi,
    >
    > I'd like to make a function to manipulate objects of a
    > given class.
    >
    > Let's say there are two objects, called MyPoint1
    > and MyPoint2. They are both of the class
    > cCustomPoint and contain an x an y attribute.
    >
    > The function should assign a value for x to a passed
    > object (MyPoint1 or MyPoint2).
    >


    class cCustomPoint
    {
    public int getX() { return x; }
    public int getY() { return y; }
    public int setX(int x) { this.x = x; }
    public int setY(int y) { this.y = y; }

    private int x, y;
    }

    class cCustomPointManager
    {
    ...
    public void passXTocCustomPoint(int x, cCustomPoint ccp)
    {
    ccp.setX(x);
    }
    ...
    }

    ...
    public static void main(String[] args)
    {
    ...
    cCustomPointManager ccpm = new cCustomPointManager();
    ...
    cCustomPoint MyPoint1 = new ..., MyPoint2 = new ...;
    ...
    ...
    ccpm.passXTocCustomPoint(5, MyPoint1);
    ccpm.passXTocCustomPoint(6, MyPoint2);
    ...
    ...

    >
    > How should I make this function so it knows which
    > object to manipulate and the value to be assigned?
    >


    I'm not sure what you are asking here. As the sample code [a very contrived
    example at that] shows, you normally know which object you want manipulated
    when you pass it as an argument to a method.

    I hope this helps [though you may need to rephrase your question]

    Anthony Borla
    Anthony Borla, Nov 28, 2003
    #2
    1. Advertising

  3. Roderik

    Jose Rubio Guest

    Something like

    manipulate( cCustomPoint aPoint )
    {
    aPoint.setX( whatever );

    etc. etc. etc.
    }

    So you would call this method passing the object you want like this:

    manipulate( MyPoint1 );

    --
    Jose Rubio
    Lead Consultant
    Airphoria
    http://www.airphoria.com


    "Roderik" <> wrote in message
    news:...
    > Hi,
    >
    > I'd like to make a function to manipulate objects of a given class.
    >
    > Let's say there are two objects, called MyPoint1 and MyPoint2.
    > They are both of the class cCustomPoint and contain an x an y
    > attribute.
    >
    > The function should assign a value for x to a passed object (MyPoint1
    > or MyPoint2).
    >
    > How should I make this function so it knows which object to manipulate
    > and the value to be assigned?
    >
    > Kind regards,
    >
    > Roderik
    Jose Rubio, Nov 28, 2003
    #3
  4. Roderik

    Roderik Guest

    On Fri, 28 Nov 2003 21:04:56 GMT, "Anthony Borla"
    <> wrote:

    >
    >"Roderik" <> wrote in message
    >news:...
    >> Hi,
    >>
    >> I'd like to make a function to manipulate objects of a
    >> given class.
    >>
    >> Let's say there are two objects, called MyPoint1
    >> and MyPoint2. They are both of the class
    >> cCustomPoint and contain an x an y attribute.
    >>
    >> The function should assign a value for x to a passed
    >> object (MyPoint1 or MyPoint2).
    >>

    >
    > class cCustomPoint
    > {
    > public int getX() { return x; }
    > public int getY() { return y; }
    > public int setX(int x) { this.x = x; }
    > public int setY(int y) { this.y = y; }
    >
    > private int x, y;
    > }
    >
    > class cCustomPointManager
    > {
    > ...
    > public void passXTocCustomPoint(int x, cCustomPoint ccp)
    > {
    > ccp.setX(x);
    > }
    > ...
    > }
    >
    > ...
    > public static void main(String[] args)
    > {
    > ...
    > cCustomPointManager ccpm = new cCustomPointManager();
    > ...
    > cCustomPoint MyPoint1 = new ..., MyPoint2 = new ...;
    > ...
    > ...
    > ccpm.passXTocCustomPoint(5, MyPoint1);
    > ccpm.passXTocCustomPoint(6, MyPoint2);
    > ...
    > ...
    >
    >>
    >> How should I make this function so it knows which
    >> object to manipulate and the value to be assigned?
    >>

    >
    >I'm not sure what you are asking here. As the sample code [a very contrived
    >example at that] shows, you normally know which object you want manipulated
    >when you pass it as an argument to a method.
    >
    >I hope this helps [though you may need to rephrase your question]
    >
    >Anthony Borla
    >


    Yes, this is okay.

    thanks a lot.
    Roderik, Nov 28, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ce
    Replies:
    1
    Views:
    2,960
  2. john smith
    Replies:
    1
    Views:
    280
  3. JoeC
    Replies:
    6
    Views:
    307
    kwikius
    Oct 5, 2006
  4. 7stud
    Replies:
    11
    Views:
    667
    Dennis Lee Bieber
    Mar 20, 2007
  5. Jerry C.
    Replies:
    8
    Views:
    206
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page