aspose file tools*
The moose likes Beginning Java and the fly likes Basic object creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Basic object creation" Watch "Basic object creation" New topic
Author

Basic object creation

Qasim Rashid
Greenhorn

Joined: Oct 16, 2011
Posts: 5
Hi, I am very new to Java (and coding in general) and I am having some problems with creating objects and getting them to work in methods.
Just as a basic task I am trying to create a class called ThreeVector and I am trying to get it to store and retrieve the z y and z components of a vector as a single object so that I can use them in various methods such as getting the magnitude of the vector, the scalar product of two vectors etc...
So far all the things I have tried have not worked either because my object has been created in the wrong way for some reason or my method cannot call the object as a valid argument
Any pointers as to how this should be laid out would be appreciated
Stefaan Dutry
Ranch Hand

Joined: Sep 17, 2010
Posts: 32

Hello and welcome to the Ranch.

Could you post us the code of what you think is your closest try so far?
That way we can spot what the problem is and work from there to help you.
I think that way it will benefit your learning much more if we start from what you have so far.

Regards,
Stefaan
Qasim Rashid
Greenhorn

Joined: Oct 16, 2011
Posts: 5
package module2;

public class ThreeVector {

public ThreeVector(int i, int j, int k) {

}
double magnitude (int xx, int yy, int zz){
double y;
y = Math.sqrt((xx*xx)+(yy*yy)+(zz*zz));
return y;
}
public static void main(String[] args) {
ThreeVector vector1 = new ThreeVector(2,3,2);
System.out.println(""+vector1);
double gh = (2,3,2);


This is what I have so far, atm its nothing coherent just the leftover of the doodles I have done while trying to get the syntax to work
Stefaan Dutry
Ranch Hand

Joined: Sep 17, 2010
Posts: 32

Ok,

Let's start at the beinning.
let me repost your code in code tags so it's easier to follow :


I see you provide a Consturctor for you class ThreeVector.
This constructor tells it to create a ThreeVector based on 3 integer parameters. First problem i spot here, is that no matter what you pass through to the constructor, nothing happens with it.
To solve this, i propose you make 3 instance variables (i, j and k) and mark them private. Instance are directly under the class declaration, meaning not in any method.
example:

Then in your constructor you need to assign the values passed to the instance variables of the current object:

Now we have an object that actualy contains the values that we passed to it.

With the method at line 8 (magnitude(int xx, int yy, int zz)) i assume you want to calculate the magnitude of the current ThreeVector object.
If that is the case you don't need to pass it any parameters, you can use the instance variables i, j and k and you can change line 8 to :


At line 16 you write out your object. What actualy gets Written out is what is returned from the ThreeVector's toString() method. Which, because it is not defined is the one inherited from it's superclass, Object (every class inherits from Object).
If you want something meaningfull when you print the object, then simply override the toString() method to have it return something usefull. Something like :


At line 17 i think you want to calculate the magnitude of the ThreeVector. This can be done by calling the method magnitude on the ThreeVector object vector1 and, if you want to do something with the value, assign it to a variable:



I hope this was clear enough.
If it wasn't, post your new version of your code with the additional question.

Regards,
Stefaan
Qasim Rashid
Greenhorn

Joined: Oct 16, 2011
Posts: 5
Thanks that does help a lot. Makes a few things clearer to me as to how to set things out.
One thing that I am still not so sure about is when you say:
"Then in your constructor you need to assign the values passed to the instance variables of the current object"
I am not so sure what you mean. I cant picture what my constructor should look like when I do that
Stefaan Dutry
Ranch Hand

Joined: Sep 17, 2010
Posts: 32



Each object (instance of the class created by using the new operator (new ThreeVector()) has it's own set of instance variables.
When you use the constructor, which, in this case, has 3 parameters, you are saying, create me a ThreeVector type object with the parameters i provide.
Those parameters only get meaning when they are actualy assigned to the object in some way, otherwise those local variables just disapear after the constructor is done executing.

If we don't assign those instance variables the values in the constructor, they will defaulted to 0 (instance variable default value for primitive int type).
Qasim Rashid
Greenhorn

Joined: Oct 16, 2011
Posts: 5
Ahh ok I see, so each time I call the ThreeVector object I would have to specify the values within it using the this.i method?
Or after that I could just write something on the lines of
ThreeVector vector1 = new ThreeVector(i,j,k)?
Stefaan Dutry
Ranch Hand

Joined: Sep 17, 2010
Posts: 32

When you write, for example:

and you have this constructor:


A new ThreeVector object gets created
In the constructor the following happens:
  • the instance variable i of the new object is set to 2 (this.i = 2;)
  • the instance variable j of the new object is set to 3 (this.j = 3;)
  • the instance variable k of the new object is set to 2 (this.k = 2;)

  • After the object is created (and it's constructor completes) the variable vector1 gets assigned the new object.

    See the attachment for a visualisation of the result of the line of code in the stack and heap.

    I hope this clears things out a bit.


    Regards,

    Stefaan



    [Thumbnail for stack_heap.png]

    Qasim Rashid
    Greenhorn

    Joined: Oct 16, 2011
    Posts: 5
    Ahh ok yes I see that definitely clears it up for me
    Thank you very much for all your help and time
    Regards

    Qasim
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Basic object creation
     
    Similar Threads
    Object Serialization
    Vectors(again)
    Collections
    Sorting a Vector by variable in the Object
    Garbage Collection - Practice