i need help to fix the code in bold as i am trying to make my remove method remo
ID: 3808030 • Letter: I
Question
i need help to fix the code in bold as i am trying to make my remove method remove a specific elemnt but for some reason i can't get it right so please help me correct it and tellme how i did wrong thank you and so long as you can make the test work with the method without changing the plus i'll be glad
thank you
the class that contains the remove method i wanna fix
package test2;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import javafx.scene.Node;
public class testing<T> {
protected Node <T> head;
protected Node<T> tail;
protected int size;
private T[] data;//Array in which the elements in this list are stored.
private ArrayList<T> list;
public Iterator<T>iterator;
public testing() {
size =0;
}
/**
* this class keeps track of each element information
* @author
*
*/
class Node <T> {
private T data;
private Node<T> next;
private Node <T>prev;
Node (T data, Node <T> next,Node<T> prev) {
this.setData(data);
this.setNext(next);
this.setPrev(prev);
}
public T getData() {return data;}
public void setData(T data) {this.data = data;}
public Node<T> getNext() {return next;}
public void setNext(Node<T> next) {this.next = next; }
public Node <T> getPrev() {return prev; }
public void setPrev(Node <T> prev) {this.prev = prev;} }
public testing<T> addToEnd(T data) {
Node<T> newNode = new Node<T>(data, null, getTail());
if(getTail()!=null)
{getTail().setNext(newNode);}
setTail(newNode);
if(getHead()==null)
{
setHead(newNode);
}
size++;
System.out.println(data);
return this;}
public testing<T> addToFront(T data) {
Node<T> newNode = new Node<T>(data, getHead(), getTail());
if(getHead()!=null)
{getHead().setPrev(newNode);}
if(getTail()==null)
{
setTail(newNode);
}
setHead(newNode);
size++;
System.out.println(data);
return this;}
public T getFirst() {
if(getHead()!=null)
return getHead().data;
else return null;}
public T getLast() {
if(getTail()!=null)
return getTail().data;
else return null;
}
public void remove(String string, Comparator<T> comparator) {
Node<T> previous = getTail();
Node <T>current = getHead();
while (current != null)
{
if(current.getData().equals(comparator))
{
current = current.next;
if(previous == null)
{
previous = current;
size--;
}
else
{
previous.next = current;
size--;
}
}
else
{
previous = current;
current = current.next;
}
}
size--;
}
public Node <T> getHead() {
return head;
}
public void setHead(Node <T> head) {
this.head = head;
}
public Node<T> getTail() {
return tail;
}
public void setTail(Node<T> tail) {
this.tail = tail;
}}
its test
package test2;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class testingTest {
testing<String> linkedString;
testing<Double> linkedDouble;
StringComparator comparator;
DoubleComparator comparatorD;
@Before
public void setUp() throws Exception {
linkedString = new testing<String>();
linkedString.addToEnd("Hello");
linkedString.addToEnd("World");
comparator = new StringComparator();
//STUDENT: Use the linkedDouble for the STUDENT tests
linkedDouble = new testing<Double>();
linkedDouble.addToEnd(15.0);
linkedDouble.addToEnd(100.0);
comparatorD = new DoubleComparator();
}
@After
public void tearDown() throws Exception {
linkedString = null;
comparator = null;
}
@Test
public void testAddToEnd() {
assertEquals("World", linkedString.getLast());
linkedString.addToEnd("End");
assertEquals("End", linkedString.getLast());
}
@Test
public void testAddToEndSTUDENT(){
//test addToEnd for the linkedDouble
assertEquals("World", linkedString.getLast());
linkedString.addToEnd("End");
assertEquals("End", linkedString.getLast()); }
@Test
public void testAddToFront() {
assertEquals("Hello", linkedString.getFirst());
linkedString.addToFront("Begin");
assertEquals("Begin", linkedString.getFirst());
}
@Test
public void testAddToFrontSTUDENT(){
//test addToFront for the linkedDouble
assertEquals("Hello", linkedString.getFirst());
linkedString.addToFront("Begin");
assertEquals("Begin", linkedString.getFirst()); }
@Test
public void testRemove() {
// remove the first
assertEquals("Hello", linkedString.getFirst());
assertEquals("World", linkedString.getLast());
linkedString.addToFront("New");
assertEquals("New", linkedString.getFirst());
linkedString.remove("New", comparator);
assertEquals("Hello", linkedString.getFirst());
//remove from the end of the list
linkedString.addToEnd("End");
assertEquals("End", linkedString.getLast());
linkedString.remove("End", comparator);
assertEquals("World", linkedString.getLast());
//remove from middle of list
linkedString.addToFront("Begin");
assertEquals("Begin", linkedString.getFirst());
assertEquals("World", linkedString.getLast());
linkedString.remove("Hello", comparator);
assertEquals("Begin", linkedString.getFirst());
assertEquals("World", linkedString.getLast());
}
private class StringComparator implements Comparator<String>
{
@Override
public int compare(String arg0, String arg1) {
// TODO Auto-generated method stub
return arg0.compareTo(arg1);
}
}
private class DoubleComparator implements Comparator<Double>
{
@Override
public int compare(Double arg0, Double arg1) {
// TODO Auto-generated method stub
return arg0.compareTo(arg1);
}
}
}
Explanation / Answer
HI, Please find implementation of remove method.
I can not test testingTest class because you have not posted testing modules.
import java.util.Comparator;
import java.util.Iterator;
public class testing<T> {
protected Node <T> head;
protected Node<T> tail;
protected int size;
public Iterator<T>iterator;
public testing() {
size =0;
}
/**
* this class keeps track of each element information
* @author
*
*/
class Node <T> {
private T data;
private Node<T> next;
private Node <T>prev;
Node (T data, Node <T> next,Node<T> prev) {
this.setData(data);
this.setNext(next);
this.setPrev(prev);
}
public T getData() {return data;}
public void setData(T data) {this.data = data;}
public Node<T> getNext() {return next;}
public void setNext(Node<T> next) {this.next = next; }
public Node <T> getPrev() {return prev; }
public void setPrev(Node <T> prev) {this.prev = prev;} }
public testing<T> addToEnd(T data) {
Node<T> newNode = new Node<T>(data, null, getTail());
if(getTail()!=null)
{getTail().setNext(newNode);}
setTail(newNode);
if(getHead()==null)
{
setHead(newNode);
}
size++;
System.out.println(data);
return this;}
public testing<T> addToFront(T data) {
Node<T> newNode = new Node<T>(data, getHead(), getTail());
if(getHead()!=null)
{getHead().setPrev(newNode);}
if(getTail()==null)
{
setTail(newNode);
}
setHead(newNode);
size++;
System.out.println(data);
return this;}
public T getFirst() {
if(getHead()!=null)
return getHead().data;
else return null;}
public T getLast() {
if(getTail()!=null)
return getTail().data;
else return null;
}
public void remove(T data, Comparator<T> comparator) {
Node <T>current = getHead();
if(current == null)
return;
// if head of the node has same value equal to data, then forward head
if(comparator.compare(data, current.getData()) == 0){
head = current.getNext();
if(head == null) // there were only one element
tail = null;
return;
}
while(current.getNext() != null){
if(comparator.compare(data, current.getNext().getData()) == 0){
current.setNext(current.getNext().getNext());
break;
}
current = current.getNext();
}
size--;
}
public Node <T> getHead() {
return head;
}
public void setHead(Node <T> head) {
this.head = head;
}
public Node<T> getTail() {
return tail;
}
public void setTail(Node<T> tail) {
this.tail = tail;
}}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.