Question: 1. Since the elements of a sparse vector are a pair of numbers, it is logical to design a structure to store an element of
1. Since the elements of a sparse vector are a pair of numbers, it is logical to design a structure to store an element of a vector. Write a class called Element which stores the index and value of an element of a vector. Download the file Element.java for the framework of the Element class. The following are the only methods 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 which would allow the Element class to implement the compareTo method to compare one element with another element in order to easily sort the elements of a sparse vector in the proper order, the element with the smallest index up to the element with the largest exponent.
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]. You cannot add any additional data members to the Element class. You cannot add any nested classes to the Element class.
2. Write a class called SparseVector which stores a sparse vector. A SparseVector is NOT a linked list. The SparseVector class has a data member which is a linked list storing the elements of a single sparse vector. A SparseVector does NOT store more than one sparse vector. Therefore, each instance of a SparseVector stores a single unique sparse vector. Store the elements of the sparse vector in increasing index order. The following are the methods 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 may add any additional methods as you see fit.
g. You cannot add any nested classes to the SparseVector class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
