0

I am a newbie to Java, just been fiddling with the code for a while.

public class ThreeVector {
private double x,y,z;   // definign local variables 

public ThreeVector(){} // a constructor that has no input

public ThreeVector (double va1,double va2, double va3){va1=x;va2=y;va3=z;};// creatign a constructor , so can be used for calling by a method later
// Takes 3 values 

public double magnitude (){
    double y1= Math.sqrt(x*x+y*y+z*z);
    return y1 ; // finds the magnitude of a vector
}

public ThreeVector unitv(){

    ThreeVector unitv= new ThreeVector ();
unitv.ThreeVector(x/magnitude(),y/magnitude(),z/magnitude());
}

Now here is where I get stuck. I created an object unitV so I could call the ThreeVector constructor, but the compiler keeps saying to create a new method for ThreeVector. Not sure whats going on...

6
  • How did you create the object? Can you add the code that is trying to use ThreeVector? Commented Jul 31, 2013 at 17:12
  • Read javabeginner.com/learn-java/java-constructors Commented Jul 31, 2013 at 17:15
  • You have a method defined as public ThreeVector unitv()... but does not return an instance of a ThreeVector. I think you want to remove the unitv method and do something like Luiggi suggests. Commented Jul 31, 2013 at 17:19
  • As a matter to style, you should consider making the class final (so it can't be subclassed) and probably the doubles in it too. Code using your ThreeVector class will be much easier to understand and debug if ThreeVector is immutable. You will have to make the no-arg constructor explicitly set 'x = 0.0; y = 0.0; z = 0.0' (assuming that is the intent of the no-arg version). Commented Jul 31, 2013 at 17:23
  • If you do decide that ThreeVector should be immutable there is probably no point having the no-arg constructor BTW. Define a 'public static final ThreeVector ZERO = new ThreeVector(0.0, 0.0, 0.0);'. Now you have a constant which represents the 'zero' vector. Commented Jul 31, 2013 at 17:28

3 Answers 3

6

A constructor can be called only using the new keyword. What you're doing here:

unitv.ThreeVector(x/magnitude(),y/magnitude(),z/magnitude());

is calling a method called ThreeVector, so the compiler complains that there's no such method in your ThreeVector class.

To fix this, you must use the ThreeVector constructor with the arguments to create your unitv instance instead:

public ThreeVector unitv(){
    ThreeVector unitv = new ThreeVector(x/magnitude(),y/magnitude(),z/magnitude());
    //and, of course, return this ThreeVector instance
    return unitv;
}

And this code can be shorten to

public ThreeVector unitv() {
    return new ThreeVector(x/magnitude(),y/magnitude(),z/magnitude());
}

But since you can have your x, y and z with 0 value at the same time, it would be better to change the logic in your unitv method to first get the magnitude value and doing an evaluation, this in order to avoid a division by 0:

public ThreeVector unitv() {
    double magnitude = magnitude();
    if (magnitude != 0) {
        return new ThreeVector(x/magnitude, y/magnitude, z/magnitude);
    }
    return new ThreeVector(0, 0, 0);
}

By the way, your constructor logic is wrong, you're assigning the fields values to the arguments, it should be the other way around:

public ThreeVector (double va1,double va2, double va3) {
    x = va1;
    y = va2;
    z = va3
}
Sign up to request clarification or add additional context in comments.

3 Comments

ThreeVector unitv= new ThreeVector (); I mean isnt that what the above line is doing ? Sorry for my ignorance just completely new to this
@RobinPhilip in the first sentence of my answer, I'm explaining that you can call a constructor using the new keyword. When you do unitv.ThreeVector(...) you're not calling the constructor with three parameters, instead calling a method called ThreeVector that doesn't exists, thus the compiler is giving you problems.
Thanks LUiggi got it , and all the others :)
0

No you need to directly call the constructor:

ThreeVector unitv= new ThreeVector(x,y,z);

Only after this you can call the magnitude method

Also your constructor does the assignments the wrong way:

Should be: public ThreeVector (double va1,double va2, double va3){x = va1;y = va2;z=va3;}

Comments

0

You need to initialize a new ThreeVector(va1, va2, va3).

Attention

Your code in the constructor is wrong.

{
    // instance field assigned to argument        
    va1=x;va2=y;va3=z;
}

... should be:

{
    // argument assigned to instance field
    x = va1;
    y = va2;
    z = va3;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.