Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Can someone explain this tutorial for me?

    Hi everyone!
    I am still quite the beginner with java.
    So I was browsing through a java tutorial by oracle and something does not make sense.
    Basically what I dont understand is how java sees the width height and area of the rectangles since it's not defined?(please check below code)


    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);
    }
    }

  • #2
    New Coder
    Join Date
    Feb 2012
    Posts
    26
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Same thing with the x and y of rectangle two?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    In the future please wrap your code in [php][/php] or [code][/code] tags to preserve the formatting.

    I don't understand your question. I see both rectOne and rectTwo declared and instantiated. Width, height, x, and y are all valid properties of java.awt.Rectangle; however, origin is not. Neither is the constructor new Rectangle(Point, int, int);. There is also no getArea on a java.awt.Rectangle object.

    So given what you have here, I'd expect that there is a custom class called Rectangle you have used that either extends a more generic shape or Rectangle directly. If it compiles you know that its custom, if it throws errors on symbols for getArea and origin, then it is attempting to use java.awt.Rectangle.


  •  

    Posting Permissions

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