Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Adding two vectors...

807569Sep 14 2006 — edited Sep 15 2006
I'm trying to make a method which adds two vectors, but am running into trouble.

The specific problem I am getting is an error stating that the constructor NumVector(double[]) is undefined when I try to make a new numVector "sumVector."

I included my whole code, but the specific problem lies within the sum method.
public class NumVector {

        //array of vectors:
        private double vector[];
        private static final int DEFAULT_SIZE = 3; //default size set to 3. 

        public NumVector(int size){
                vector = new double[size];
        }

        public NumVector (NumVector v){
                vector = new double[v.vector.length];
                for(int i = 0; i < vector.length; i++)
                    vector[i] = v.vector; 
}

public NumVector(){
vector = new double [DEFAULT_SIZE];
}

public void setValue(int i, double value){
//Sets value of "i"th spot to value inputted.

vector[i] = value;
}

public double getValue (int i){
if (i >= 0 && i < vector.length)
return vector[i];
else
return 0;
}


public NumVector sum(NumVector other){

double sumArray[] = vector;

for (int i = 0; i < sumArray.length; i++){
sumArray[i] = vector[i] + other.getValue(i);

}

NumVector sumVector = new NumVector(sumArray);

return sumVector;
}


public static void main(String[] args) {
// TODO Auto-generated method stub


}



}

is there a better way to approach adding two vectors?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 13 2006
Added on Sep 14 2006
25 comments
221 views