Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started