[thelist] Java

Lee Kowalkowski lee.kowalkowski at googlemail.com
Tue Jul 29 06:11:41 CDT 2014


On 28 July 2014 20:11, Santilal Parbhu <santilal at scorpioneng.co.nz> wrote:

> Hi
>
> I have spent a few days trying to understand Lee's comments but I seem to
> be
> going backwards.


Oh no!


> I have looked up the recommended resources and I am
> currently looking at
> http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html.
>
> My brain is completely tied up in knots.  I think I will need to break it
> all down into understandable chunks (if that exists for me).
>
> In the reference above is the following code.
>
> public class Point {
>     public int x = 0;
>     public int y = 0;
>     //constructor
>     public Point(int a, int b) {
>         x = a;
>         y = b;
>     }
> }
>
> What does this class actually do?  Is the code complete or is it just an
> excerpt for iluustration?
>

Well, it doesn't do a lot does it?  x and y are initialised to 0 when you
create a new Point object.  The default values for integers is 0 anyway.
 Since there's no default constructor if you write your own, and the
constructor overwrites x and y with the values of a and b, there wasn't
much point in giving them default values in this example.

The other answer to "What does this class actually do?" is that it's
modelling a 2D co-ordinate.  The benefit here is that one could add more
functionality to this class in future, perhaps to calculate the distance
between two Points or to transform it in some way.

Is it useful?  Well, suppose you're writing an entire API and you always
wanted people to specify x and y values every time.  You could write
methods that accepted integers x and y everywhere, sure, but wrapping them
inside a Point class is saying that x and y are so closely related when
they represent a point in two-dimensional space, one value is not usable
without the other, so they should live together, inside the same object.


> The first two lines declare and initialise the variables x and y to 0.
> The next line is a constructor which creates an instance of the class Point
> with variables a and b.  Then x and y are assigned to a and b.  But why not
> just write public Point (int x, int y).  This does not make sense to me.
>

You can write public Point(int x, int y) in fact that's pretty normal,
however, then you have two x variables, the one that's local to the
constructor method, and one that belongs to the class.  Then you would need
to use the 'this' keyword to tell them apart:

public Point(int x, int y) {
    this.x = x;
    this.y = y;
}

This indeed looks like more work, but this technique is common practice.
(Just naming them x and y does no magic, if you were expecting them to
automatically pollinate point.x and point.y, that doesn't happen).

Also I thought that you only create an instance of a class when you want to
> use it like in the next example on the link:
>

Yep, you create an object of type Point, if you want to use the Point class.


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;
>     }
> }
>
> Line 1 declares the class Rectangle.  Line 4 declares an instance of the
> class Rectangle and it is named origin.
>

No, an instance of the Point class, called origin!

I think
>
> public Rectangle() {
>         origin = new Point(0, 0); means create a new instance of Point,
> call
> it origin and use 0,0 as the values (but for what??)


...as the values for x and y, according to the Point constructor (even
though they're called a and b, which isn't helpful)


> Also what is the
> reason for the statement public Rectangle().  What is the relationship
> between these two classes Rectangle and Point.
>

Right, look again, there are 4 constructor methods, meaning, there are 4
different ways to create a new instance of the Rectangle class.

You can have all default values:

Rectangle r = new Rectangle();

You can pass in just the origin

Point p = new Point(100, 50);
Rectangle r = new Rectangle(p);

...or the dimensions; new Rectangle(200, 300); ...or both: new Rectangle(p,
200, 300);

This class is a model for a 2D rectangle.  There are many ways to model
things, a model is just an approach to solve a problem, but the simplicity
of a solution is often at the mercy of the way the problem has been
modelled.

The Rectangle class models a rectangle by saying it has a width, a height,
and an anchor point (origin).  There is no information about the
relationship between the rectangle and the anchor, it could be the top-left
corner, or the centre, you can't tell.

In terms of OO, the relationship between Rectancle and Point is a 'has a'
relationship, meaning a Rectangle 'has a' Point.

Each constructor creates an instance of the class, each with the same name
> origin, but each is different because of its signature.  But I can't
> understand what
>
> public Rectangle(Point p) {
>         origin = p;
>
>  or
>       origin = new Point(0, 0);
>         width = w;
>         height = h;
>
> or
>     public Rectangle(Point p, int w, int h) {
>         origin = p;
>         width = w;
>         height = h;
>
> are trying to do?
>

They are trying to offer you the flexibility to create a Rectangle object
in whatever way is most convenient to you.  You choose which constructor to
use when creating a new Rectangle, you don't have to use them all, you just
use whichever you need.

-- 
Lee


More information about the thelist mailing list