JAVA CODE: 3.2. Implement a class NVector representing a vector with n elements:
ID: 3750392 • Letter: J
Question
JAVA CODE:
3.2. Implement a class NVector representing a vector with n elements: (v[0], v[1], ...., v[n-1]), with v[i] a double number. The NVector class stores the numbers in an array double v[n] The NVector class should support the following interface: * constructor, takes dimension n and sets all elements to 0: NVector (int n) * constructor, takes another NVector and copies all data from "other": NVector (NVector other) * constructor, takes a double p[] array and copies all data to the new object: NVector (double[] v) * a VARARG constructor declared like this: public NVector (double... v); This constructor is called like this: NVector vec- new NVector(1,2,3,4); The caller passes the elements of the NVector directly as arguments to the constructor. In this example the result is the initialization with elements [1,2,3,4] You have to find out how to write the constructor code by checking the Java tutorial online. * a method that returns the vector's size: int length( * accessor, returns element with index i: double get(int i) * the equals method that compares two NVector object:s * a method that returns a new copy of an NVector with just one element changed NVector set(int i, double x) Example if w=NVector( [3,2,0]), w.set(1,5) returns a new NVector with elements [3,-5,0] NVector add(NVector other). e.g. NVector ([1,2,3]).add (NVector([4,5,6])) returns new NVector([5,7,9]). double sprod(NVector other). * add, returns a new NVector with the sum of this vector and the other * sprod, returns a double with the scalar product of this vector and another NVector, e.g. NVector([1,2,3]).sprod (NVector([4,5,6])) returns 1*4+2*5+3*6 32 * string representation: String toString() This could return a string such as "[2.63 3.14 1.41]" Note that add() and sprod() require vectors of the same size. You must specify the class contract - pre/postconditions/invariant - with javadoc comments, for all methods. For some, the precondition is trivial (i.e. 'none You can choose to put exceptions in the contract OR write preconditions OR use assertions. Be **consistent** b) Writea main) method that shows how the methods above are used on same sample NVector objects.Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
public class NVector {
private double[] v;
public NVector(int n){
v = new double[n];
}
public NVector(NVector other){
v= new double[other.v.length];
for(int i = 0; i < v.length;i++)
v[i] = other.v[i];
}
public NVector(double...v1){
v= new double[v1.length];
for(int i = 0; i < v.length;i++)
v[i] = v1[i];
}
public int length(){
return v.length;
}
public double get(int i){
return v[i];
}
public boolean equals(NVector other){
if(length() == other.length()){
for(int i = 0; i < length(); i++){
if(v[i] != other.get(i))
return false;
}
return true;//all matched
}
return false;
}
public NVector set(int i, double x){
NVector copy = new NVector(this);
copy.v[i] = x;
return copy;
}
public NVector add(NVector other){
if(length() != other.length()){
System.out.println("Can not add two unequal sized vectors");
return null;
}
else {
NVector result = new NVector(length());
for(int i = 0; i < v.length; i++)
result.v[i] = v[i] + other.v[i];
return result;
}
}
public double sprod(NVector other){
if(length() != other.length()){
System.out.println("Can not find scalar product two unequal sized vectors");
return 0;
}
else {
double result = 0;
for(int i = 0; i < v.length; i++)
result += v[i] * other.v[i];
return result;
}
}
public String toString(){
String s = "[";
for(int i = 0; i < v.length; i++)
s+= " " + v[i];
s += "]";
return s;
}
public static void main(String[] args) {
NVector v1 = new NVector(1, 2, 3, 4);
NVector v2 = new NVector(5, 6, 7, 8);
System.out.println("v1 is " + v1);
System.out.println("v2 is " + v2);
System.out.println("Assiging v3 = v2.set(1,2) ");
NVector v3 = v2.set(1, 2);
System.out.println("v3 is " + v3);
System.out.println("length of v3 = " + v3.length());
System.out.println("v1 + v3 (addition) = " + v1.add(v3));
System.out.println("v1 * v3 (scalar prod) = " + v1.sprod(v3));
}
}
output
-----
v1 is [ 1.0 2.0 3.0 4.0]
v2 is [ 5.0 6.0 7.0 8.0]
Assiging v3 = v2.set(1,2)
v3 is [ 5.0 2.0 7.0 8.0]
length of v3 = 4
v1 + v3 (addition) = [ 6.0 4.0 10.0 12.0]
v1 * v3 (scalar prod) = 62.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.