Reputation: 19
So for a uni assignment I'm supposed to write a class "Vector" that has a double array as an attribute. An object of the type "Vector" is supposed to represent a Vector in an n-dimensional space.
Now I have to write a method that takes an integer n and creates an object of the type "Vector" and initializes the attribute with a double array of the size n, so that I can use the Vector for calulating a scalar product later on.
The method that calulates the scalar product is supposed to take a Vector q and then calculate the scalar product of the current vector with the Vector q, so basically if "p" was my current Vector, then p.scalarproduct(q)
is supposed to calculate the scalar product of p and q.
So far, this is my unfinished code:
public class Vector {
private static double[] vec;
public Vector(double[] vec){
this.vec = vec;
}
static Vector newWithDimension(int n){
vec = new double[n];
Vector vector = new Vector(vec);
return vector;
}
double scalarproduct(Vector q){
Vector p = vector;
double scalarproduct = 0.0;
if (q.length == p.length){
for(int i = 0; i < q.length; i++){
scalarproduct = scalarproduct + q[i] * p[i];
}
}
return scalarproduct;
}
}
As you can see I am still missing a few things and there are a few problems with the code:
For one, I don't know how to write the method scalarproduct, because its supposed to only take one vector as a parameter, but is supposed to calculate the scalar product of two vectors.
I don't get how I am supposed to use the current vector inside the method without having it as a parameter.
Also, in the method scalarproduct, I get an error when trying to use q.length
, because q is a Vector and not an array, even though I initialize the attribute with an array.
I'm quite inexperienced with programming and I have no idea if my implementation is even on the right path or what I'm supposed to do at this point, I'm simply just confused. Any help with solving this assignment is appreciated.
Upvotes: 0
Views: 5295
Reputation: 558
I don't get how I am supposed to use the current vector inside the method without having it as a parameter.
this
refers to the current object, so you can use that to access member variables. You probably want to make vec
not static and refer to it instead of p
.
So replace
private static double[] vec;
with
private double[] vec;
and then you can refer to it as this.vec
.
Upvotes: 0
Reputation: 35011
You need to make vec
non-static. That way, each vector has it's own double[].
Then, you could redo scalarProduct like this:
double scalarproduct(Vector q){
double scalarproduct = 0.0;
if (q.vec.length == vec.length){
for(int i = 0; i < q.vec.length; i++){
scalarproduct = scalarproduct + q.vec[i] * vec[i];
}
}
return scalarproduct;
}
but what I'd do is make this a static function
static double scalarproduct(Vector q, Vector p){
double scalarproduct = 0.0;
if (q.vec.length == p.vec.length){
for(int i = 0; i < q.vec.length; i++){
scalarproduct = scalarproduct + q.vec[i] * p.vec[i];
}
}
return scalarproduct;
}
Upvotes: 3