Object's properties changed

V

Vasu

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
 
V

Vasu

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.
 
L

Lew

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|
|_____|_____|
|===========|
 
B

blmblm

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top