Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question won\'t take you more than 15 mins and junit test provided. . Compl

ID: 3593685 • Letter: T

Question

This question won't take you more than 15 mins and junit test provided.

. Complete the 3 methods it contains. The value each method must return is drawn or described below:

buildEmptySingle()

This method should return an empty singly-linked list (a linked list of length 0)

buildListWith4Strings()

This method should return the first node in the linked list shown below:
Singly-linked list with 4 nodes containing: Tom, Dick, Harry, and Oh my!

buildListWith4Ints()

This method should return the first node in the linked list shown below:
Singly-linked list with 4 nodes containing: 45, 42, 109, and -12

----------------------------------------------------------------

-------------------------------------------------------

Entry class(need for the test)

package edu.buffalo.cse116;

/**
* Represents a node ("entry") in a singly-linked list.
*
* @author Matthew Hertz
* @version 1.0
* @param <E> Type of data stored within the Entry
*/
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;
}
}

----------------------------

Test file

package edu.buffalo.cse116;

import org.junit.Assert;
import org.junit.Test;

public class LinkedListConstructorTest
{
@Test
public void testBuildEmptySingle()
{
Entry<Double> emptyList = LinkedListConstructor.buildEmptySingle();
Assert.assertNull("An empty singly linked list is really just a null reference.", emptyList);
}
  
@Test
public void testBuildListWith4Ints()
{
int[] elements = { 45, 42, 109, -12 };
Entry<Integer> header = LinkedListConstructor.buildListWith4Ints();
Assert.assertNotNull("Did not instantiate or return the first node for a linked list with 4 elements.", header);
for (int i = 0; i < elements.length - 1; i++)
{
Assert.assertEquals("Incorrect value for the " + (i + 1) + "th element in the list", elements[i], ((Integer)header.getElement()).intValue());
Assert.assertNotNull("Should have another node in the linked list, but the next field in the " + (i + 1) + "th node is null!",
header.getNext());
header = header.getNext();
}
Assert.assertEquals("Incorrect value for the last element in the list", elements[(elements.length - 1)],
((Integer)header.getElement()).intValue());
Assert.assertNull("Should not have another node in the linked list when we are at the last element in the list", header.getNext());
}
  
@Test
public void testBuildListWith4Strings()
{
String[] elements = { "Tom", "Dick", "Harry", "Oh my!" };
Entry<String> header = LinkedListConstructor.buildListWith4Strings();
Assert.assertNotNull("Did not instantiate or return the first node for a linked list with 4 elements.", header);
for (int i = 0; i < elements.length - 1; i++)
{
Assert.assertEquals("Incorrect value for the " + (i + 1) + "th element in the list", elements[i], header.getElement());
Assert.assertNotNull("Should have another node in the linked list, but the next field in the " + (i + 1) + "th node is null!",
header.getNext());
header = header.getNext();
}
Assert.assertEquals("Incorrect value for the last element in the list", elements[(elements.length - 1)], header.getElement());
Assert.assertNull("Should not have another node in the linked list when we are at the last element in the list", header.getNext());
}
}

Explanation / Answer

/**
* This class is used to construct different linked lists as part of the week #8 weekly assignment. The goal is for
* students to get a better understanding of how linked lists work and can be programmed.
*
* @author Matthew Hertz
*/
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 = 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", "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> tom = new Entry<>("Tom");
Entry<String> dic = new Entry<>("Dick");
Entry<String> harry = new Entry<>("Harry");
Entry<String> ohMy = new Entry<>("Oh my!");
  
tom.setNext(dic);
dic.setNext(harry);
harry.setNext(ohMy);
return tom;
  
}

/**
* 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> fortyFive = new Entry<>(45);
Entry<Integer> fortyTwo = new Entry<>(42);
Entry<Integer> Entry<>(109);
Entry<Integer> minusTwelwe = new Entry<>(-12);
  
fortyFive.setNext(fortyTwo);
fortyTwo.setNext(oneNotNine);
oneNotNine.setNext(minusTwelwe);
return fortyFive;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote