Results 1 to 4 of 4
Thread: Objects
- 11-11-2009, 09:16 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
Objects
public class CreateObjectDemo {
public static void main(String[] args) {
//Declare and create a point object
//and two rectangle objects.
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);
//display rectOne's width, height, and area
System.out.println("Width of rectOne: " +
rectOne.width);
System.out.println("Height of rectOne: " +
rectOne.height);
System.out.println("Area of rectOne: " + rectOne.getArea());
//set rectTwo's position
rectTwo.origin = originOne;
//display rectTwo's position
System.out.println("X Position of rectTwo: "
+ rectTwo.origin.x);
System.out.println("Y Position of rectTwo: "
+ rectTwo.origin.y);
//move rectTwo and display its new position
rectTwo.move(40, 72);
System.out.println("X Position of rectTwo: "
+ rectTwo.origin.x);
System.out.println("Y Position of rectTwo: "
+ rectTwo.origin.y);
}
}
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int a, int b) {
x = a;
y = b;
}
}
public 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;
}
// a method for computing the area of the rectangle
public int getArea() {
return width * height;
}
}
This is not my own program but got it from net.. :D
My doubt is
I'm confused with this line
Rectangle rectTwo = new Rectangle(50, 100); After the execution of this line
Its assigns the value for rectTwo.origin.x=0 and rectTwo.origin.y=0 but the values for rectOne.origin.x=23 and rectOne.origin.y=94
But what adds more confusion to me is this line
rectTwo.move(40, 72);
this line changes the rectOne.origin.x=40, rectTwo.origin.x=40 & rectOne.origin.y=72, rectTwo.origin.y=72
So the origin of both the objects has the same copies which contradicts this statement Rectangle rectTwo = new Rectangle(50, 100); which displays differnent values of origin. Why the value of rectOne.origin.x and rectOne.origin.y hasn't changed to 0??
please help me with this. I am just a beginner. Correct me where i am going wrong.
- 11-11-2009, 10:26 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
//set rectTwo's position
rectTwo.origin = originOne;
This is where the origin of rectTwo is set to the same object as that of rectOne.
They now have the same origin.
- 11-11-2009, 01:32 PM #3
Member
- Join Date
- Nov 2009
- Posts
- 2
- Rep Power
- 0
@Tolls-Thanks for your reply.
So if originone hasn't assigned to rectTwo.origin will both the rectTwo and rectOne will have different origin??
- 11-11-2009, 02:36 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Yep.
The origin of rectOne is referencing the same object as originOne originally due to the constructor you used. That of rectTwo starts off as a reference to a new object.
It's only when you change the origin reference in rectTwo to that of originOne that it then references the same object as rectOne. So, if you don't do that then the two references will be different.
Similar Threads
-
read txt file,with some records, create objects and store objects in tables of a db.
By stamv in forum JDBCReplies: 1Last Post: 01-22-2009, 04:25 PM -
Two Objects
By losintikfos in forum New To JavaReplies: 3Last Post: 11-14-2008, 07:04 PM -
1 to 1 Objects
By this.that in forum New To JavaReplies: 4Last Post: 08-07-2008, 10:09 PM -
how many objects ?
By kevinsong in forum Advanced JavaReplies: 16Last Post: 07-16-2008, 05:59 PM -
Help with Objects!
By Shorinhio in forum New To JavaReplies: 1Last Post: 07-10-2007, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks