3.2. Implement a class NVector representing a vector with n elements: (v[0], v[1
ID: 3750350 • Letter: 3
Question
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
package DS;
// Class NVector definition
public class NVector
{
// Instance variables to store numbers
double v[];
// Parameterized constructor to dynamically allocate memory to array v
public NVector(int n)
{
// Dynamically allocates memory of size n
v = new double[n];
}// End of parameterized constructor
// Parameterized constructor to create a duplicate copy of the parameterized object
public NVector(NVector other)
{
// Dynamically allocates memory of size parameter object vector length
v = new double[other.v.length];
// Loops till vector size
for(int c = 0; c < other.v.length; c++)
// Assigns each element of parameter vector to instance vector
v[c] = other.v[c];
}// End of parameterized constructor
// Parameterized constructor to create a vector from parameterized double vector
public NVector(double []v)
{
// Dynamically allocates memory of size parameter vector length
this.v = new double[v.length];
// Loops till vector size
for(int c = 0; c < v.length; c++)
// Assigns each element of parameter double vector to instance vector
this.v[c] = v[c];
}// End of parameterized constructor
// Parameterized constructor to create a vector with variable length parameter values
public NVector(int ...v)
{
// Dynamically allocates memory of size parameter variable length vector
this.v = new double[v.length];
// Loops till variable length argument length
for(int c = 0; c < v.length; c++)
// Assigns each parameter values to instance vector
this.v[c] = v[c];
}// End of parameterized constructor
// Method to return the length of the vector
int length()
{
return v.length;
}// End of method
// Method to return i index position value of the vector
double get(int i)
{
return v[i];
}// End of method
// Method to return true if parameter vector is equals to instance vector
boolean equals(NVector other)
{
// Loops till vector size
for(int c = 0; c < v.length; c++)
{
// Checks if current index position value of the instance vector
// is not equals to parameter vector value returns false
if(v[c] != other.v[c])
return false;
}// End of for loop
// Otherwise return true
return true;
}// End of method
// Method to create a new object and set parameter value x at index position i
// and return the new copy of the object
NVector set(int i, double x)
{
// Creates a temporary object using parameterized constructor
NVector temp = new NVector(v.length);
// Loops till vector size
for(int c = 0; c < v.length; c++)
// Checks if loop variable value is equals to parameter index value i
if(c == i)
// Assign parameter x value at i index position of the temporary object
temp.v[i] = x;
// Otherwise assign each element of instance vector to temporary object
else
temp.v[c] = v[c];
// Return the temporary object
return temp;
}// End of method
// Method to create a new object to store the sum of instance vector data with parameter object vector data
// Return the new copy of the object
NVector add(NVector other)
{
// Creates a temporary object using parameterized constructor
NVector temp = new NVector(v.length);
// Loops till vector size
for(int c = 0; c < v.length; c++)
// Adds each element at c index position of instance vector
// with c index position of parameter object vector
// and stores it at c index position of temporary vector
temp.v[c] = v[c] + other.v[c];
// Return the temporary object
return temp;
}// End of method
// Method to calculate the product of instance vector data with parameter object vector data
// Return the product value
double sprod(NVector other)
{
// To store the product result
double result = 0;
// Loops till vector size
for(int c = 0; c < v.length; c++)
// Calculates the product
result += v[c] * other.v[c];
// Returns the product result
return result;
}// End of method
// Overrides the toString() method to return the vector data
public String toString()
{
// Assigns opening square bracket to result string
String result = "[ ";
// Loops till vector size
for(int c = 0; c < v.length; c++)
// Concatenates each element of the instance vector with space to result string
result += v[c] + " ";
// Concatenates closing square bracket to result string
result += "]";
// Returns the result string
return result;
}// End of method
// main method definition
public static void main(String[] args)
{
// Creates a double vector
double vec[] = {1, 2, 3, 4};
// Creates different objects using different parameterized constructor
NVector NVector(4);
NVector two = new NVector(vec);
NVector three = new NVector(7, 8, 9, 10, 11, 12, 13);
NVector four = new NVector(three);
// Displays the object one vector data
System.out.print(" Object ONE Using constructor with integer size: NVector(5) " + one);
// Calls the method to display length
System.out.print(" Length: " + one.length());
// Calls the method to set the value 20 at 2nd index position of object one
one = one.set(2, 20);
// Displays the object one vector data
System.out.print(" Object after setting values 20 at 2 index position: " + one);
// Displays the object two vector data
System.out.print(" Object TWO Using constructor with double array vec: NVector(vec) " + two);
// Calls the method to display length
System.out.print(" Length: " + two.length());
// Calls the method to set the value 10 at 1st index position of object two
two = two.set(1, 10);
// Displays the object two vector data
System.out.print(" Object after setting values 10 at 1 index position: " + two);
// Displays the object three vector data
System.out.print(" Object THREE Using constructor with variable length parameter: NVector(7, 8, 9, 10) " + three);
// Calls the method to display length
System.out.print(" Length: " + three.length());
// Displays the object four vector data
System.out.print(" Object FOUR Using constructor with another object: NVector(two) " + four);
// Calls the method to display length
System.out.print(" Length: " + four.length());
// Calls the method to compare object three with object four
System.out.print(" Object THREE equals to Object FOUR: " + three.equals(four));
// Calls the method to compare object two with object three
System.out.print(" Object TWO equals to Object THREE: " + two.equals(three));
// Calls the method to add object two with object one
one = two.add(one);
System.out.print(" Object ONE after " + one);
// Calls the method to calculate product of object three with object four
System.out.print(" Object ONE after three.sprod(four);: " + three.sprod(four));
}// End of main method
}// End of class
Sample Output:
Object ONE
Using constructor with integer size: NVector(5)
[ 0.0 0.0 0.0 0.0 ]
Length: 4
Object after setting values 20 at 2 index position: [ 0.0 0.0 20.0 0.0 ]
Object TWO
Using constructor with double array vec: NVector(vec)
[ 1.0 2.0 3.0 4.0 ]
Length: 4
Object after setting values 10 at 1 index position: [ 1.0 10.0 3.0 4.0 ]
Object THREE
Using constructor with variable length parameter: NVector(7, 8, 9, 10)
[ 7.0 8.0 9.0 10.0 11.0 12.0 13.0 ]
Length: 7
Object FOUR
Using constructor with another object: NVector(two)
[ 7.0 8.0 9.0 10.0 11.0 12.0 13.0 ]
Length: 7
Object THREE equals to Object FOUR: true
Object TWO equals to Object THREE: false
Object ONE after [ 1.0 10.0 23.0 4.0 ]
Object ONE after three.sprod(four);: 728.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.