Question: Write JUnit unit test methods in file TestNVector.java for these methods: (BELOW IS THE CODE FOR NVector.java) - the vararg constructor - equals - get

Write JUnit unit test methods in file TestNVector.java for these methods: (BELOW IS THE CODE FOR NVector.java) - the vararg constructor - equals - get - add() - sprod() - toString()

Write a method testXXX() for testing each operation, like this, for add():

// testing NVector.add @Test public void testAdd() { System.out.println("run test add"); double a = 1, b = 2, c = -3, d = 4;

NVector x = new NVector(a, b); NVector y = new NVector(c, d);

NVector result = x.add(y);

NVector expected = .... // fill in missing code

// test condition using the equals() method: Assert.assertTrue(result.equals(expected)); }

Use the equals() method for verification.

.

.

.

package q1; 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 one = new 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 = two.add(one);: " + 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!