Complete the 3 methods.The value each method must return is described below: bui
ID: 3594074 • Letter: C
Question
Complete the 3 methods.The value each method must return is described below:
buildEmptySingle() //This method must return an empty singly-linked list(a linked list of lenght 0)
buildListWith4Strings() // This method should return th first node in the linked list below : Singly-linked list with 4 nodes containg : Tom, Dick, Harry and Oh my!
buildListWith4Ints() // This method should return th first node in the linked list below : Singly-llinked list with 4 nodes containing: 45, 42, 109 and -12
public class LinkedListConstructor {
/**
* This method should instantiate and return an empty linked list. You will
* need to instantiate and connect any Entry instances yourself.
*
* @return Singly linked list that does not contain any elements.
*/
public static Entry<Double> buildEmptySingle() {
Entry<Double> retVal;
}
/**
* This method should instantiate and return a singly linked list with 4
* elements. In order, the elements should be the Strings "Tom", "Dick",
* "Harry", "Oh my!". You will need to instantiate and connect any Entry
* instances yourself.
*
* @return Singly linked list that contains the 4 elements in the correct
* order
*/
public static Entry<String> buildListWith4Strings() {
Entry<String> retVal;
}
/**
* This method should instantiate and return a singly-linked list with 4
* elements. In order, the elements should be the Integers 45, 42, 109, -12.
* You will need to instantiate and connect any Entry instances yourself.
*
* @return Singly linked list that contains the 4 elements in the correct
* order
*/
public static Entry<Integer> buildListWith4Ints() {
Entry<Integer> retVal;
}
}
public class Entry<E> {
/** Data stored within this entry. */
private E element;
/** Reference to the entry following this one in the linked list. */
private Entry<E> next;
/**
* Creates an empty entry.
*/
public Entry() {
next = null;
element = null;
}
/**
* Creates a entry storing the specified element.
*
* @param elem Element to which the new entry should refer
*/
public Entry(E elem) {
next = null;
element = elem;
}
/**
* Get the next entry in the linked list.
*
* @return Reference to the entry following this one
*/
public Entry<E> getNext() {
return next;
}
/**
* Specify that the given entry should follow the current one in the linked
* list.
*
* @param entry entry which should follow the current one
*/
public void setNext(Entry<E> entry) {
next = entry;
}
/**
* Get the element stored within this entry.
*
* @return Element referred to by the entry
*/
public E getElement() {
return element;
}
/**
* Direct the entry to store a new element.
*
* @param elem New element which this entry should store
*/
public void setElement(E elem) {
element = elem;
}
}
Explanation / Answer
Please find my implementation.
public class LinkedListConstructor {
/**
* This method should instantiate and return an empty linked list. You will
* need to instantiate and connect any Entry instances yourself.
*
* @return Singly linked list that does not contain any elements.
*/
public static Entry<Double> buildEmptySingle() {
Entry<Double> retVal;
retVal = new Entry<>();
return retVal;
}
/**
* This method should instantiate and return a singly linked list with 4
* elements. In order, the elements should be the Strings "Tom", "Dickk",
* "Harry", "Oh my!". You will need to instantiate and connect any Entry
* instances yourself.
*
* @return Singly linked list that contains the 4 elements in the correct
* order
*/
public static Entry<String> buildListWith4Strings() {
Entry<String> retVal;
retVal = new Entry<>("Tom");
retVal.setNext(new Entry<>("Dickk"));
retVal.getNext().setNext(new Entry<>("Harry"));
retVal.getNext().getNext().setNext(new Entry<>("Oh my!"));
return retVal;
}
/**
* This method should instantiate and return a singly-linked list with 4
* elements. In order, the elements should be the Integers 45, 42, 109, -12.
* You will need to instantiate and connect any Entry instances yourself.
*
* @return Singly linked list that contains the 4 elements in the correct
* order
*/
public static Entry<Integer> buildListWith4Ints() {
Entry<Integer> retVal;
retVal = new Entry<>(45);
retVal.setNext(new Entry<>(42));
retVal.getNext().setNext(new Entry<>(109));
retVal.getNext().getNext().setNext(new Entry<>(-12));
return retVal;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.