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