[thelist] Java

Santilal Parbhu santilal at scorpioneng.co.nz
Mon Aug 4 04:56:59 CDT 2014


Hi

I am still trying to understand Java and I think I am now making some
headway.  Please if someone has time could you please answer the following
or confirm if my thinking is correct.

Here is the code I am working with:

/** Comment
 * Displays "Hello World!" to the standard output. 

 */
public class HelloWorld2 {
     String output = "";
     static HelloWorld2 helloObj;  

       public HelloWorld2(){
           output = "Hello World";
     	 } 

     public String printMessage(){
            return output;
      } 
 
      public static void main (String args[]) {             //Line 2
			helloObj = new HelloWorld2();
            System.out.println(helloObj.printMessage());
  }

1.	 The first three lines are declarations.
2.	The next three lines are a constructor that sets the value of output
to "Hello World".
3.	I am still having trouble with the next three lines - 
    
	public String printMessage(){
	            return output;
	      }

I think this is a call to a method called printMessage().  Where is this
defined or is it a system method?  Why do you need it anyway.  Why can't you
just use the line System.out.println(helloObj.printMessage()); to print
output.  It already has a value.  If this was PHP you would just write
something like $output = "Hello World";  print $output.

4. 	The instance helloObj is created by the new.  This calls the
constructor defined above.  But why would you have an instance of a class
created within that same class.  I thought the idea of OOP was to create a
class, and then each time you want to use this in another piece of code, you
just create an instance of the class in the other piece of code.  For
instance, if I created a class call graph, then each time I needed a graph
in other bit of code, I would just create an instance of it.  What am I
missing.

5.	Does the line helloObj = new HelloWorld2(); have to be embedded in
the main method or can you have it anywhere?

Thanks in advance for your help.


Santilal Parbhu
Scorpion Engineering Limited
PO Box 171
Alexandra
Phone: +64 3 440 2100
Mobile: +64 21 2655991
Email: santilal at scorpioneng.co.nz
Web: www.scorpioneng.co.nz
-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Santilal Parbhu
Sent: Wednesday, 30 July 2014 7:18 a.m.
To: thelist at lists.evolt.org
Subject: Re: [thelist] Java

Hi Lee

Thanks again for a very thorough reply.

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

Yes I understand now - the penny just dropped.  x,y are local to the class.
a,b are global as declared by "Public".

>No, an instance of the Point class, called origin!  Yes I think I knew
that.  I think writing "Rectangle" was a typo.  Sorry.

>     public Rectangle() {
>         origin = new Point(0, 0);
>     }

Yes I see now.  Again the penny has just dropped.  This says to create an
instance of Rectangle using the parameters within the curly brackets.

Thanks again.  I will digest this further after work today.

Cheers.

Santilal

Santilal Parbhu
Scorpion Engineering Limited
PO Box 171
Alexandra
Phone: +64 3 440 2100
Mobile: +64 21 2655991
Email: santilal at scorpioneng.co.nz
Web: www.scorpioneng.co.nz
-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Lee Kowalkowski
Sent: Tuesday, 29 July 2014 11:12 p.m.
To: thelist at lists.evolt.org
Subject: Re: [thelist] Java

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

* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester
and archives of thelist go to: http://lists.evolt.org
Workers of the Web, evolt ! 

-- 

* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester
and archives of thelist go to: http://lists.evolt.org
Workers of the Web, evolt ! 



More information about the thelist mailing list