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

QUESTION:: For the class SimpleLinkedList2 write a method: public int getTotal()

ID: 3584917 • Letter: Q

Question

QUESTION:: For the class SimpleLinkedList2 write a method:

public int getTotal()
The method sums up all integer values in the calling object of SimpleLinkedList2 and returns the sum. Your code CANNOT call any other methods in this class or any other class. It means that you have to implement it from scratch and walk down the list of Nodes “in-person”.
Hint: Your code is inside the class SimpleLinkedList2 and has direct access to the data fields in the calling object of SimpleLinkedList2.

public class SimpleLinkedList2 {
/** the head pointer to point to the first node in the list */
private Node head;

/**constructor to create an empty list */
public SimpleLinkedList2() {
head = null;
}

/**insert the new integer value stored in newItem to the end of the list */
public void add(int newItem) {
//create a new node for the integer value in newItem
Node newNode = new Node(newItem);
//the link to current node in the list
Node current;

//search for the last node in the list
if (head == null) { //if the list is empty
head = newNode;
}
else {
//search for the last node in the list
current = head;
while (current.next != null) {
current = current.next;
}
//make the new node as the last in the list
current.next = newNode;
}
}

/**Insert the new integer value stored in variable newItem to the list.
* The new value will be the immediate following neighbor after the node referenced by here.
*/
private void add(int newItem, Node here) {
Node temp = new Node(newItem);
temp.next = here.next;
here.next = temp;

}

@Override
public String toString() {
String result = "";
Node current = head;

while (current != null) {
result += current.dataItem + "==>";
current = current.next;
}
return result;
}

/**
* Static nested class representing a data node in the linked list
* The class is placed inside the class SimpleLinkedList2
* The class cannot directly access instance methods like toString(...), add(...) in the
* enclosing class SimpleLinkedList.
*
* @author Cindy
*/
private static class Node {
/**The data item (an integers) stored in the node */
private int dataItem;
/**the link to the immediate following neighbor */
private Node next;

/**Create a node for the item (an integer) */
public Node(int item) {
dataItem = item;
next = null;
}
}
}

Explanation / Answer

public class SimpleLinkedList2 { /** the head pointer to point to the first node in the list */ private Node head; /**constructor to create an empty list */ public SimpleLinkedList2() { head = null; } public int getTotal() { int sum = 0; if(head == null) return sum; Node current = head; while (current != null) { sum += current.dataItem; current = current.next; } return sum; } /**insert the new integer value stored in newItem to the end of the list */ public void add(int newItem) { //create a new node for the integer value in newItem Node newNode = new Node(newItem); //the link to current node in the list Node current; //search for the last node in the list if (head == null) { //if the list is empty head = newNode; } else { //search for the last node in the list current = head; while (current.next != null) { current = current.next; } //make the new node as the last in the list current.next = newNode; } } /**Insert the new integer value stored in variable newItem to the list. * The new value will be the immediate following neighbor after the node referenced by here. */ private void add(int newItem, Node here) { Node temp = new Node(newItem); temp.next = here.next; here.next = temp; } @Override public String toString() { String result = ""; Node current = head; while (current != null) { result += current.dataItem + "==>"; current = current.next; } return result; } /** * Static nested class representing a data node in the linked list * The class is placed inside the class SimpleLinkedList2 * The class cannot directly access instance methods like toString(...), add(...) in the * enclosing class SimpleLinkedList. * * @author Cindy */ private static class Node { /**The data item (an integers) stored in the node */ private int dataItem; /**the link to the immediate following neighbor */ private Node next; /**Create a node for the item (an integer) */ public Node(int item) { dataItem = item; next = null; } } }

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