Question

In: Computer Science

I Have posted my Java code below. Fix the toString, add, and remove implementations so that...

I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work.

Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO.

Test (Main)

List list = new SparseList<>(); list.add("0");
list.add("1");
list.add(4, "4");

will result in the following list of size 5: [0, 1, null, null, 4].

list.add(3, "Three");
will result in the following list of size 6: [0, 1, null, Three, null, 4].

However,

list.set(3, "Three);
is going to produce a list of size 5 (unchanged): [0, 1, null, Three, 4].

When removing an element from the list above, via list.remove(1)); the result should be the following list of size 4: [0, null , Three, 4]

Using the other constructor, List list = new SparseList<>(); would just result in all the null values produced by remove, get, set and toString be replaced with the specified value, e.g., [0, 1, null, null, 4]. Consider completing the implementation of your class with the default constructor only first, and then make the needed modifications.

import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.ListIterator;

public class SparseList implements List{

private int endIndex = 0;

private HashMap list;

public SparseList() {

list = new HashMap<>();

}

public SparseList(E[] arr) {

list = new HashMap<>();

for(int i = 0; i

list.put(i, arr[i]);

}

endIndex = arr.length - 1;

}

@Override

public boolean add(E e) {

endIndex++;

list.put(endIndex, e);

return true;

}

@Override

public void add(int index, E element) {

list.put(index, element);

}

@Override

public E remove(int index) {

return list.remove(index);

}

@Override

public E get(int index) {

return list.get(index);

}

@Override

public E set(int index, E element) {

E previous = list.get(index);

list.put(index, element);

return previous;

}

@Override

public int size() {

return endIndex + 1;

@Override

public void clear() {

list.clear();

}

@Override

public boolean isEmpty() {

return list.isEmpty();

}

@Override

public String toString() {

return list.toString();

}

}

8.4.3

Solutions

Expert Solution

Please try below code. If you still facing error requesting you provide the complete code. This would help us to identify and fix the problem in a faster manner.

-----------------------------------------------------------------------

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;


public class SparseList implements List{

private int endIndex = 0;
private HashMap list;

public SparseList() {
this(10);
}

/*public SparseList(int lenOfArr) {
list = new HashMap<>();
for(int i = 0; i<=lenOfArr; i ++){
list.put(i, list[i]);
}
endIndex = arr.length - 1;
}*/

@Override
public boolean add(Object e) {
if (endIndex==0){
list.put(endIndex, e);
}
else {
endIndex++;
list.put(endIndex, e);
}
return true;
}

@Override
public void add(int index, Object element) {
list.put(index, element);
}

@Override
public Object remove(int index) {
return list.remove(index);
}

@Override
public Object get(int index) {
return list.get(index);
}

@Override
public Object set(int index, Object element) {
List previous = list.get(index);
list.put(index, element);
return previous;
}

@Override
public int size() {
return endIndex + 1;
}

@Override
public void clear() {
list.clear();
}

@Override
public boolean isEmpty() {
return list.isEmpty();
}

@Override
public String toString() {
return list.toString();
}

@Override
public List subList(int startIndex, int endIndex) {
// TODO
  
}

}


Related Solutions

How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T,...
Can you fix my code and remove the errors? Thank you!! ^^ ////////////////////////////////////////////////////////////////////////////////////////////////////// public class StackException<T, size> extends Throwable { private final T[] S = null ; public StackException(String s) { } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); int top = 0; return S[top]; } private boolean isEmpty() { return false; } public T pop() throws StackException { T item; if (isEmpty()) throw new StackException("Stack underflow."); int top = 0; item = S[top];...
in java: In my code at have my last two methods that I cannot exactly figure...
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far: public static void main(String[] args) { // TODO Auto-generated method stub...
The code following is what I have so far. It does not meet my requirements. My...
The code following is what I have so far. It does not meet my requirements. My problem is that while this program runs, it doesn't let the user execute the functions of addBook, isInList or compareLists to add, check, or compare. Please assist in correcting this issue. Thank you! Write a C++ program to implement a singly linked list of books. The book details should include the following: title, author, and ISBN. The program should include the following functions: addBook:...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
I have the following code for my java class assignment but i am having an issue...
I have the following code for my java class assignment but i am having an issue with this error i keep getting. On the following lines: return new Circle(color, radius); return new Rectangle(color, length, width); I am getting the following error for each line: "non-static variable this cannot be referenced from a static context" Here is the code I have: /* * ShapeDemo - simple inheritance hierarchy and dynamic binding. * * The Shape class must be compiled before the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT