Question
The following code compiles and runs. The problem is that two tests in the JUnit test class aren't working properly. They are testing the contains
The following code compiles and runs. The problem is that two tests in the JUnit test class aren't working properly. They are testing the contains method and indexOf method from the LinkedList class. I have highlighted the two tests below. Any suggestions on how to fix this?
--------------------------------------------------------------------------------------
import static org.junit.Assert.*; import org.junit.Test;
/** * A JUnit test class for class LinkedList. */ public class LinkedListTest {
Dog fifi = new Dog("Fifi", 12, 8); Dog butch = new Dog("Butch", 10, 10); Dog leonard = new Dog("Leonard", 22, 13); Dog spot = new Dog("Spot", 17, 9);
@Test public void test() { LinkedList
//test method for contains method @Test public void test2() { LinkedList
//test method for add method with index @Test public void test3() { LinkedList
//test method for indexOf method @Test public void test4() { LinkedList
//test method for Iterator hasNext method @Test public void test5() { LinkedList
@Test public void test6() { LinkedList
} // end class LinkedListTest
---------------------------------------------------------------------------------------
public class LinkedList
public LinkedList() { itsFirstNode = null; itsLastNode = null; size = 0; }
public Iterator
/** * A doubly-linked list * @param element */ public void add(T element) {
Node
if (itsFirstNode == null) { itsFirstNode = node; itsLastNode = node; } else { itsLastNode.setNextNode(node); node.setPriorNode(itsLastNode);//prior node set from new node to last node itsLastNode = node; } size++; }
/** * A doubly-linked list * @param element */ public void add(T element, int index) {
int counter = 0; Node
while (current.getNextNode() != null ) {
if (counter == index - 1 ) break;
current = current.getNextNode(); counter++; } newNode.setNextNode(current.getNextNode()); Node
/** * A doubly-linked list * @param element */ public T get(int index) {
int counter = 0; Node
while (current.getNextNode() != null ) {
if (counter == index) break;
current = current.getNextNode(); counter++; } return current.getData(); }
/** * A doubly-linked list * @param element * @return if element is in the list result is true, false if not */ public boolean contains(T element) {
Node
if (itsFirstNode == null) return res;
else {
while (head != null) {
if(head.getData() == element){ res = true; break; } head.setNextNode(head.getNextNode()); } } return res; }
/** * A doubly-linked list * @param element * @return the index of the element if it is in the list, -1 if not found */ public int indexOf(T element) {
Node
if(itsFirstNode == null) return -1;
else {
while (head != null) {
if (head.getData() == element) { break; } head.setNextNode(head.getNextNode()); index++; } } return index; }
/** * We cannot get Java iterator to an user defined object unless implementing * iterable interface, therefore the return type is Node
Node
if (itsFirstNode == null) return null;
else {
while (head != null) {
if (head.getData() == element) { break; } head.setNextNode(head.getNextNode()); } } return head; }
public String toString() {
String returnVal = ""; Node
if (size != 0 ) {
while (current.getNextNode() != null ) { returnVal += current.toString(); returnVal += " "; current = current.getNextNode(); } returnVal += current.toString(); } return returnVal; } // end toString
class Node
public Node(T data) { itsNext = null; itsPrior = null; this.data = data; }
public T getData() { return this.data; }
public Node
public Node
public void setNextNode(Node
public void setPriorNode(Node
public String toString() { return data.toString(); }
} // end of Node
} // end LinkedList
---------------------------------------------------------------------------------------
public class Iterator
private LinkedList
public Iterator(LinkedList
/** * @return true if there is a "next" element, otherwise returns false */ public boolean hasNext() { if (myCurrentNode != null) return true; return false; }
/** * @return true if there is a "prior" element, otherwise returns false */ public boolean hasPrior() { if (myCurrentNode.getPriorNode() != null) return true; return false; }
/** * @return the "next" node (really the current one) * and moves the Iterator forward by one node */ public T next() { T data = myCurrentNode.getData(); myCurrentNode = myCurrentNode.getNextNode(); return data; }
/** * @return the "prior" node (really the current one) * and moves the Iterator backward by one node */ public T prior() { T data = myCurrentNode.getData(); myCurrentNode = myCurrentNode.getPriorNode(); return data; }
/** * Sets this iterator to point to the last Node in the list */ public void setToEnd() { while (myCurrentNode.getNextNode() != null) myCurrentNode = myCurrentNode.getNextNode(); }
} //end Iterator
---------------------------------------------------------------------------------------
public class Dog {
private int weight; private int heightAtShoulder; private String name;
public Dog (String name, int w, int h) { this.name = name; this.weight = w; this.heightAtShoulder = h; }
public String getName() {return this.name;} public int getHeight() {return this.heightAtShoulder;} public int getWeight() {return this.weight;}
public void bark() {System.out.println("woof");}
public String toString() { return "Dog: " + name + " , height: " + heightAtShoulder + " weight: " + weight; }
@Override public boolean equals(Object o) {
if (o instanceof Dog) { //now we can do checks Dog tempDogRef = (Dog)(o); if (tempDogRef.getName().equals(this.name) && tempDogRef.getHeight() == this.heightAtShoulder && tempDogRef.getWeight() == this.weight) return true; } return false; }
} // end class Dog
---------------------------------------------------------------------------------------
public class ListRunner {
public static void main(String[] args) {
Dog fifi = new Dog("Fifi", 12, 8); Dog butch = new Dog("Butch", 10, 10); Dog leonard = new Dog("Leonard", 22, 13); Dog spot = new Dog("Spot", 17, 9);
LinkedList
myDogList.add(fifi); System.out.println("Added Fifi to list."); System.out.println(myDogList); System.out.println();
myDogList.add(butch); myDogList.add(leonard); myDogList.add(spot); System.out.println("Added Butch, Leonard, and Spot to list."); System.out.println(myDogList); System.out.println();
Dog jack = new Dog("Jack", 18, 18); myDogList.add(jack,1); System.out.println("Added Jack to list."); System.out.println(myDogList); System.out.println();
// the following lines build Iterators, and provide an example // of how you would use an Iterator to step through this data // structure Iterator
System.out.println("Iterating using Iterators!!!"); while (iter1.hasNext()) { Dog foo = iter1.next(); System.out.println(foo); }
System.out.println();
// In a doubly-linked list, an iterator can move either forward or backward, // so this should print the list out in reverse order if you've correctly // implemented everything System.out.println("Iterating backwards using Iterators!!!"); iter2.setToEnd(); while (iter2.hasPrior()) { Dog foo = iter2.prior(); System.out.println(foo); } Dog foo = iter2.prior(); System.out.println(foo);
}// end main
} // end class ListRunner
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started