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]...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements...
Can you fix my code and remove the errors in java language. public class LinkedStack<T> implements Stack<T> { private Node<T> top; private int numElements = 0; public int size() { return (numElements); } public boolean isEmpty() { return (top == null); } public T top() throws StackException { if (isEmpty()) throw new StackException("Stack is empty."); return top.info; } public T pop() throws StackException { Node<T> temp; if (isEmpty()) throw new StackException("Stack underflow."); temp = top; top = top.getLink(); return temp.getInfo();...
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 python I have my code written and I want just 4 functions to be fix...
In python I have my code written and I want just 4 functions to be fix in my code according to rule. My code is running but it has some problem regarding to rules. (I have did all the other things so you do not have to worry about other functions) 1) all the players has to play until he/she reaches to at least 500 points in first round. When user reach 500 points for the first time, user may...
Java question: I need to fix a point class (code below) Thank you! /** * A...
Java question: I need to fix a point class (code below) Thank you! /** * A point, implemented as a location without a shape. */ public class Point extends Location { // TODO your job // HINT: use a circle with radius 0 as the shape! public Point(final int x, final int y) { super(-1, -1, null); assert x >= 0; assert y >= 0; } }
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed...
(java)Fix the code in this program. Fix lines, add lines… Comment each line that you fixed … what you did to fix it. import java.util.Scanner; public static void main(String[] args) { // This program Converts grade Points into a Letter grade. int gradePoints == 00; // Declare variable and assign initial value System.out.printl("Enter Grade Points: "; //Input gradePoints; if ( gradePoints >= -42 ) { System.out.println("Grade = A"); } // if true, then ends here, if false drop to next...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT