Question
I have to write 2 Java classes called Element, SparseVector . Element stores the index and value of an element of a vector. SparseVector stores
I have to write 2 Java classes called Element, SparseVector. Element stores the index and value of an element of a vector. SparseVector stores a sparse vector and has a data member which is a linked list storing the elements of a single sparse vector.
The following are the only method of the Element class:
a. The constructor for the class.
b. The get and set methods for the index.
c. The get and set methods for the value.
d. The Element class implements the Comparable interface:
public class Element implements Comparable
e. The toString method to properly print an element. For example, if the index of an element is 4 and the value is -36.4 then the toString method would print [4, -36.4].
and The following are the only method of the SparseVector class:
a. The constructor for the class.
b. The add method: public SparseVector add(SparseVector sv), which performs the addition of two sparse vectors. For example, if A and B are sparse vectors then A.add(B) returns a SparseVector which is A+B.
c. The subtract method: public SparseVector subtract(SparseVector sv), which performs the subtraction of two sparse vectors. For example, if A and B are sparse vectors then A.subtract(B) returns a SparseVector which is A-B.
d. The dot method: public double dot(SparseVector sv), which performs the dot product of two sparse vectors. For example, if A and B are sparse vectors then A.dot(B) returns a double which is AB.
e. The toString method to properly print the sparse vector. The elements of the sparse vector must appear in increasing index order. A sparse vector with no elements prints the string empty vector. For example, if the sparse vector has the elements [1, 76.02], [4, -36.4], [5, 7.537], and [10, -2.19] then the toString method would print ([1, 76.02], [4, -36.4], [5, 7.537], [10, -2.19]).
f. You can add any additional methods as you see fit.
I just finished Element but I have no idea how to write SparseVector. Can you check if my Element is correct and help me to write SparseVector?
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class Element implements Comparable
public Element(int i, double v) { this.index = i; this.value = v; }
public int getIndex() { return index; }
public double getValue() { return value; }
public void setIndex(int i) { index = i; }
public void setValue(double v) { value = v; }
public int compareTo(Element e) { return ("[" + index + ", " + value + "]").compareTo("[" + e.index + ", " + e.value + "]"); }
public String toString() { return "[" + index + ", " + value + "]"; } }
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