import java.util.Arrays; /** Represents a linked list containing integers. */ pu
ID: 665656 • Letter: I
Question
import java.util.Arrays;
/** Represents a linked list containing integers. */
public class IntegerLinkedList {
private IntegerNode head;
/** Creates an empty linked list. */
public IntegerLinkedList() {
head = null;
}
/**
* Creates a linked list with the specified head.
*
* @param head is the head of the new linked list.
*/
public IntegerLinkedList(IntegerNode head) {
this.head = head;
}
/**
* Returns a linked list with contents passed in from an array.
*
* @param cargo is the contents the returned array is to be filled
* with.
* @return a linked list filled with the elements from cargo.
*/
public static IntegerLinkedList fromArray(int[] cargo) {
IntegerLinkedList list = new IntegerLinkedList();
for (int i = cargo.length - 1; i >= 0; i--) {
list = list.addToFront(cargo[i]);
}
return list;
}
/**
* Returns the cargo of the head element.
*
* @return the cargo of the head element.
*/
public int first() {
return head.getCargo();
}
/**
* Returns the tail of the linked list as an IntegerLinkedList
* object.
*
* @return the tail of the linked list.
*/
public IntegerLinkedList rest() {
return new IntegerLinkedList(head.getNext());
}
/**
* A predicate for testing whether the linked list is empty.
*
* @return a boolean indicating whether the linked list is empty.
*/
public boolean isEmpty() {
return head == null;
}
/**
* Adds an element to the front of the linked list.
*
* @param cargo is the item to be added to the front.
*/
public IntegerLinkedList addToFront(int cargo) {
return new IntegerLinkedList(new IntegerNode(cargo, head));
}
@Override
public boolean equals(Object other) {
if (!(other instanceof IntegerLinkedList)) {
return false;
}
IntegerLinkedList rhs = (IntegerLinkedList)other;
if (isEmpty() && rhs.isEmpty()) {
return true;
} else if (isEmpty() || rhs.isEmpty()) {
return false;
} else if (first() != rhs.first()) {
return false;
} else {
return rest().equals(rhs.rest());
}
}
/**
* Returns the number of elements in the linked list.
*
* @return the number of elements in the linked list.
*/
public int size() {
if (isEmpty()) {
return 0;
}
return 1 + rest().size();
}
}
/** Represents a node in a linked list containing integers. */
public class IntegerNode {
private int cargo;
private IntegerNode next;
public IntegerNode(int cargo, IntegerNode next) {
this.cargo = cargo;
this.next = next;
}
public int getCargo() {
return cargo;
}
public IntegerNode getNext() {
return next;
}
}
Who people can help me doing this question?
This question will the following directory structure:
|-- Makefile
|-- lib
|__ |-- checkstyle-6.5-all.jar
|__ |-- hamcrest-core-1.3.jar
|__ -- junit-4.12.jar
|-- src
|__ |-- IntegerLinkedList.java
|__ |-- IntegerLinkedListUtils.java
|__ -- IntegerNode.java
-- tests
|-- IntegerLinkedListUtilsTest.java
-- MyTestRunner.java
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class MyTestRunner {
/**
* Runs all my unit tests.
*
* @param args command line arguments, this is ignored.
*/
public static void main(String[] args) {
boolean failed = false;
Result result = JUnitCore.runClasses(IntegerLinkedListUtilsTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
failed = true;
}
if (failed) {
System.exit(1);
}
}
}
1/ You are to follow test driven development (TDD), you should write your tests in tests/IntegerLinkedListUtilsTest.java, to develop from the methods above. You actually followed TDD, and not simply wrote your tests after the fact.
2/ You will be writing your code in the source files: src/IntegerLinkedListUtils.java
public static int[] toArray(IntegerLinkedList list)
public static IntegerLinkedList addToEnd(IntegerLinkedList list, int cargo)
public static IntegerLinkedList reverse(IntegerLinkedList list)
public static IntegerLinkedList join(IntegerLinkedList first, IntegerLinkedList second)
a/ public static int[] toArray(IntegerLinkedList list) {
Your code here…
}
This method is to return an integer array containing the contents of the linked list list.
For example: if we have the linked list : 6 --> -8 --> 2 -->4 --> null
then toArray would return an array equivalent to [6,-8,2,4]
b/ public static IntegerLinkedList addToEnd(IntegerLinkedList list, int cargo) {
Your code here…
}
This methods takes a linked list list, an integer cargo, and is to return a new linked list that contains the elements of list with cargo added to the end.
For example: if list is: 6 --> -8 --> 2 --> 4 --> null and cargo was 0 then the result of addToEnd would be the new linked list : 6 --> -8 --> 2 --> 4 --> 0 --> null
c/ public static IntegerLinkedList reverse(IntegerLinkedList list) {
Your code here…
}
This method is to return a new linked list which is the reverse of list.
For example: if list is: : 6 --> -8 --> 2 --> 4 --> null
then the result of reverse will be the new linked list : 4 --> 2 --> -8 --> 6 --> null
Your implementation can only use the following methods:
•your addToEnd method from above;
•reverse in a recursive manner; and
•any of the public methods from IntegerLinkedList
d/ public static IntegerLinkedList join(IntegerLinkedList first, IntegerLinkedList second) {
Your code here…
}
This method takes two linked lists first and second, and returns a new linked list that contains the concatenation of the items in first with those from second.
For example: if first is : 6 --> -8 --> 2 --> 4 --> null and second is: -5 --> 4 --> -3 --> null
then the result from join would be the new linked list :
6 --> -8 --> 2 --> 4 --> -5 --> 4 --> -3 --> null
Your implementation can only use the following methods:
•your reverse method from above; and
•any of the public methods from IntegerLinkedList.
A common error is to neglect the test case where the linked list is empty. I strongly encourage you to add appropriate test cases to test whether you are properly handling empty linked lists.
*NOTE
•your code must passes the Checkstyle audit.
•your code has an appropriate amount of commenting.
•you have an appropriate number of tests.
•your code passes all your tests.
•your code is not overly complex.
•your implementation of the required methods is correct.
-------------------------------------------------------------------------------
This is some my code:
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import java.util.Arrays;
public class IntegerLinkedlistUtilsTest {
@Test
public void testToArrayEmpty() {
int[] actual = IntegrrLinkedListUtils.toArray(new IntegerLinkedList());
int[] expected = {};
assertTrue(Arrays.equals(actual, expected));
}
@Test
public void testToArraySingleElement() {
IntegerLinkedList list = new IntegerLinkedList ();
list = list.addToFront (3);
int[] actual = IntegrrLinkedListUtils.toArray(List());
int[] expected = {3};
assertTrue(Arrays.equals(actual, expected));
}
??More Tests here…
}
And
public class IntegerLinkedListUtils {
public static int[] toArray(IntegerLinkedList list) {
if (list.isEmpty()) {
return new int[] {};
} else {
Return new int[] {list,first()};
}
}
}
??More code here…
Explanation / Answer
this question has lot of subparts. Please post 2 more questions.
public static Node reverseLinkedList(Node currentNode)
{
// For first node, previousNode will be null
Node previousNode=null;
Node nextNode;
while(currentNode!=null)
{
nextNode=currentNode.next;
// reversing the link
currentNode.next=previousNode;
// moving currentNode and previousNode by 1 node
previousNode=currentNode;
currentNode=nextNode;
}
return previousNode;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.