"3. (5 points) Write a method for a class that uses an array
For the Vector class given below, write a method that computes the dot product of the Vector "this" object with the Vector object passed as an argument, "b". The dot-product is the sum of the products of the corresponding elements of the vectors. The Vectors must be the same length for the dot-product to be defined. For example, if a = {1,2,3,4} and b = {2,4,6,8}, the dot product would be 1*2 + 2*4 + 3*6 + 4*8 = 60.
[code=java]
public class Vector {
private double [] a;
public Vector(double newData[]) {
a=new double[newData.length];
for (int i=0; i<newData.lengt h; i++)
a[i]=newData[i];
}
public int getLength() {
return a.length;
}
public double getEntry(int index) {
return a[index];
}
public double dotProduct(Vect or b) {
// YOUR CODE GOES HERE
}
}[/code]
"
So this is my assignment. The title suggests only using arrays and the constructor takes an array but the dotProduct method takes a vector as an argument. So, I'm assuming I'm supposed to convert vector b to an array or convert array a to a vector.
So, my question is this: How do I convert to one or the other? I've looked around for examples but they don't seem to make sense or translate very well into my situation. Help, please?
For the Vector class given below, write a method that computes the dot product of the Vector "this" object with the Vector object passed as an argument, "b". The dot-product is the sum of the products of the corresponding elements of the vectors. The Vectors must be the same length for the dot-product to be defined. For example, if a = {1,2,3,4} and b = {2,4,6,8}, the dot product would be 1*2 + 2*4 + 3*6 + 4*8 = 60.
[code=java]
public class Vector {
private double [] a;
public Vector(double newData[]) {
a=new double[newData.length];
for (int i=0; i<newData.lengt h; i++)
a[i]=newData[i];
}
public int getLength() {
return a.length;
}
public double getEntry(int index) {
return a[index];
}
public double dotProduct(Vect or b) {
// YOUR CODE GOES HERE
}
}[/code]
"
So this is my assignment. The title suggests only using arrays and the constructor takes an array but the dotProduct method takes a vector as an argument. So, I'm assuming I'm supposed to convert vector b to an array or convert array a to a vector.
So, my question is this: How do I convert to one or the other? I've looked around for examples but they don't seem to make sense or translate very well into my situation. Help, please?
Comment