Results 1 to 4 of 4

Thread: Objects

  1. #1
    Silverlining is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

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

  3. #3
    Silverlining is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    @Tolls-Thanks for your reply.

    So if originone hasn't assigned to rectTwo.origin will both the rectTwo and rectOne will have different origin??

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    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

  1. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  2. Two Objects
    By losintikfos in forum New To Java
    Replies: 3
    Last Post: 11-14-2008, 07:04 PM
  3. 1 to 1 Objects
    By this.that in forum New To Java
    Replies: 4
    Last Post: 08-07-2008, 10:09 PM
  4. how many objects ?
    By kevinsong in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2008, 05:59 PM
  5. Help with Objects!
    By Shorinhio in forum New To Java
    Replies: 1
    Last Post: 07-10-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •