Object's properties changed

Discussion in 'Java' started by Vasu, Feb 12, 2011.

  1. Vasu

    Vasu Guest

    Hi There! If somebody can help me solving following problem:

    We have 3 objects (rectOne, rectTwo and rect3) of rectangle class
    which also refers to point class for starting position of rectangle.
    Now, we have defined starting position of rectangles as 23, 94, now we
    change the starting position for rectTwo rectangle to 40 and 72, but
    we find that starting position of rectOne and rect3 also change
    whereas THAT WAS NOT INTENDED, so how do I achieve it ? Here is the
    code and output :
    class Point {
    public int x = 0;
    public int y = 0;
    //constructor
    public Point(int a, int b) {
    x = a;
    y = b;
    }
    }

    class Rectangle {
    public int width = 0;
    public int height = 0;
    public Point origin;
    // four constructors
    public Rectangle() {
    origin = new Point(0, 0);
    }
    public Rectangle(Point p) {
    origin = p;
    }
    public Rectangle(int w, int h) {
    origin = new Point(0, 0);
    width = w;
    height = h;
    }
    public Rectangle(Point p, int w, int h) {
    origin = p;
    width = w;
    height = h;
    }
    // a method for moving the rectangle
    public void move(int x, int y) {
    origin.x = x;
    origin.y = y;
    }
    }

    public class CreateObjectDemo {
    public static void main(String[] args) {

    //Declare and create a point object
    //and 3 rectangle objects.
    Point originOne = new Point(23, 94);
    Rectangle rectOne = new Rectangle(originOne, 100, 20);
    Rectangle rectTwo = new Rectangle(50, 100);
    Rectangle rect3 = new Rectangle(45, 45);

    //display rectOne's y point
    System.out.println("Original y point of rectOne: " +
    rectOne.origin.y);

    //set rectTwo's position
    rectTwo.origin = originOne;

    //display rectTwo's position
    System.out.println("Original X Position of rectTwo: " +
    rectTwo.origin.x);
    System.out.println("Original Y Position of rectTwo: " +
    rectTwo.origin.y);

    //display rect3's width, height, and area
    rect3.origin = originOne;
    System.out.println("Original y point of rect3: " +
    rect3.origin.y);


    //move rectTwo and display its new position
    System.out.println("Now we are moving rectTwo to a different place");
    rectTwo.move(40, 72);
    System.out.println("New X Position of rectTwo: " +
    rectTwo.origin.x);
    System.out.println("New Y Position of rectTwo: " +
    rectTwo.origin.y);

    //display rect3's width, height, and area
    System.out.println("New y point of rect3: " + rect3.origin.y);


    //display rectOne's y point
    System.out.println("New y point of rectOne: " +
    rectOne.origin.y);


    }
    }


    Original y point of rectOne: 94
    Original X Position of rectTwo: 23
    Original Y Position of rectTwo: 94
    Original y point of rect3: 94
    Now we are moving rectTwo to a different place
    New X Position of rectTwo: 40
    New Y Position of rectTwo: 72
    New y point of rect3: 72
    New y point of rectOne: 72
     
    Vasu, Feb 12, 2011
    #1
    1. Advertising

  2. Vasu

    Vasu Guest

    On Feb 12, 12:01 pm, Peter Duniho <>
    wrote:
    > On 2/12/11 1:55 PM, Vasu wrote:
    >
    > > Hi There! If somebody can help me solving following problem:

    >
    > > We have 3 objects (rectOne, rectTwo and rect3) of rectangle class
    > > which also refers to point class for starting position of rectangle.
    > > Now, we have defined starting position of rectangles as 23, 94, now we
    > > change the starting position for rectTwo rectangle to 40 and 72, but
    > > we find that starting position of rectOne and rect3 also change
    > > whereas THAT WAS NOT INTENDED, so how do I achieve it ?  [...]

    >
    > Among other things, stop using the same Point object to describe the
    > origin for all three of your Rectangle instances.
    >
    > Frankly, I can't imagine what would motivate someone to define their own
    > custom Point and Rectangle structures anyway. What's wrong with the ones
    > already in the JDK?
    >
    > But if you're going to define your own, and you're going to use public
    > instance fields (YUCK!) that reference mutable objects, then you need to
    > make sure the code you write that uses your data structures does so in
    > the right way, by either not sharing the same mutable object among more
    > than one Rectangle, or by not mutating a mutable object that is shared.
    >
    > I don't like the public fields in the JDK types, but at least they are
    > always value types, and thus not sharable with other instances of the
    > types.  Your problem would never have even come up if you'd just stuck
    > with the JDK types.
    >
    > Pete


    Hi Peter, thanks for your prompt reply, actually I'm newbie in Java
    programming field and just learning and I just tried the example @
    Oracle / java programming basics. Thanks anyway once again and I will
    find how to achieve the objectives. But if you could suggest the code
    to be used here, will be highly appreciable.
     
    Vasu, Feb 12, 2011
    #2
    1. Advertising

  3. Vasu

    Lew Guest

    Peter Duniho wrote:
    >> Among other things, stop using the same Point object to describe the
    >> origin for all three of your Rectangle instances.
    >> ...


    Vasu wrote:
    > Hi Peter, thanks for your prompt reply, actually I'm newbie in Java
    > programming field and just learning and I just tried the example @
    > Oracle / java programming basics. Thanks anyway once again and I will
    > find how to achieve the objectives. But if you could suggest the code
    > to be used here, will be highly appreciable.


    He already gave you the answer!

    You point all three rectangles to the SAME origin. Change the origin, all
    three move. He told you not to use the same 'Point' to describe all three
    origins. That's your answer. Now thank him and implement the change.

    --
    Lew
    Ceci n'est pas une fenêtre.
    ..___________.
    |###] | [###|
    |##/ | *\##|
    |#/ * | \#|
    |#----|----#|
    || | * ||
    |o * | o|
    |_____|_____|
    |===========|
     
    Lew, Feb 12, 2011
    #3
  4. Vasu

    Guest

    In article <>,
    Peter Duniho <> wrote:
    > On 2/12/11 1:55 PM, Vasu wrote:
    > > Hi There! If somebody can help me solving following problem:
    > >
    > > We have 3 objects (rectOne, rectTwo and rect3) of rectangle class
    > > which also refers to point class for starting position of rectangle.
    > > Now, we have defined starting position of rectangles as 23, 94, now we
    > > change the starting position for rectTwo rectangle to 40 and 72, but
    > > we find that starting position of rectOne and rect3 also change
    > > whereas THAT WAS NOT INTENDED, so how do I achieve it ? [...]

    >
    > Among other things, stop using the same Point object to describe the
    > origin for all three of your Rectangle instances.
    >
    > Frankly, I can't imagine what would motivate someone to define their own
    > custom Point and Rectangle structures anyway.


    As a way to learn about writing classes?

    Based on later posts in this thread, that might not really apply in
    this situation, since the example the OP is working from assumes use
    of the library classes.

    Still, as someone who teaches programming classes I think there can be
    value for beginners in reinventing the wheel. <shrug>

    (I agree with you otherwise. "Just sayin'" about potential pedagogical
    value, maybe.)

    > What's wrong with the ones
    > already in the JDK?
    >
    > But if you're going to define your own, and you're going to use public
    > instance fields (YUCK!) that reference mutable objects, then you need to
    > make sure the code you write that uses your data structures does so in
    > the right way, by either not sharing the same mutable object among more
    > than one Rectangle, or by not mutating a mutable object that is shared.
    >
    > I don't like the public fields in the JDK types, but at least they are
    > always value types, and thus not sharable with other instances of the
    > types. Your problem would never have even come up if you'd just stuck
    > with the JDK types.
    >
    > Pete



    --
    B. L. Massingill
    ObDisclaimer: I don't speak for my employers; they return the favor.
     
    , Feb 12, 2011
    #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. Replies:
    1
    Views:
    737
    Rosanne
    Oct 11, 2005
  2. madani
    Replies:
    2
    Views:
    419
    madani
    Jan 4, 2006
  3. Brian Mitchell
    Replies:
    0
    Views:
    335
    Brian Mitchell
    Jan 12, 2006
  4. SuperZE
    Replies:
    5
    Views:
    363
    SuperZE
    Oct 6, 2008
  5. mxbrunet
    Replies:
    1
    Views:
    243
Loading...

Share This Page