i need help to fix the code in bold as i am trying to make my toarraylist method
ID: 3808152 • Letter: I
Question
i need help to fix the code in bold as i am trying to make my toarraylist method return a list of elements from my linked list and make it work with its test so please look at my code fix it and tell me what i did wrong thank you.
the class with arraylist method i need help fixin in bold
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;}
/**Returns an arraylist of the items in the list from head of list to tail of list
* @return an arraylist of the items in the list
*/
public ArrayList<T> toArrayList() {
//Node <T>newNode=new Node<T>(null, head, tail);
// = null;
ArrayList<T> list = new ArrayList<T>();
for (int i=0;i<=list.size();i++)
{
//list.get(i);
list.add(data[i]);
list.getClass();
System.out.println(data[i]);}
return list;
}
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 method
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 testToArrayList()
{
ArrayList<String> list;
linkedString.addToFront("Begin");
linkedString.addToEnd("End");
list = linkedString.toArrayList();
assertEquals("Begin", list.get(0));
assertEquals("Hello", list.get(1));
assertEquals("World", list.get(2));
assertEquals("End", list.get(3));
}
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 I have fixed ArrayList method.
I can not test with given testing code, because you have not posted all testing modules.
import java.util.ArrayList;
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--;
}
/**Returns an arraylist of the items in the list from head of list to tail of list
* @return an arraylist of the items in the list
*/
public ArrayList<T> toArrayList() {
ArrayList<T> list = new ArrayList<T>();
Node<T> current = head;
while(current != null)
{
list.add(current.getData());
System.out.println(current.getData());
current = current.getNext();
}
return list;
}
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.