Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 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].

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 { int index; double value;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

3. Describe the strategic training and development process.

Answered: 1 week ago

Question

10. Microsoft Corporation

Answered: 1 week ago

Question

4. EMC Corporation

Answered: 1 week ago