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

import java.util.Scanner; /** * Question: * * Finish the implementation of the s

ID: 3691378 • Letter: I

Question

import java.util.Scanner;

/**
* Question:
*
* Finish the implementation of the sum() method.
* This method will return the sum of all of the elements in the list,
* or 0 if the list is empty.
*
* For instance, given the list
* 1 2 3
* sum() would return 6.
*
* You will also need to add the "addToEnd()" method that you
* wrote in question 5. You can simply copy and paste that
* into this class.
*
* Only add code in between the comments that say
* "Begin your code here"
* and
* "End your code here"
*
* or immediately after
* "Paste addToEnd() method here"
*/

public class LinkedList {
   // Private inner class Node
  
   private class Node{
       int data;
   Node link;
  
   @SuppressWarnings("unused")
       public Node(){
       data = Integer.MIN_VALUE;
       link = null;
   }
  
   public Node(int x, Node p){
       data = x;
       link = p;
   }
   }
   // End of Node class
  
   public Node head;
  
   public LinkedList(){
       head = null;
   }
  
   public int sum() {
       /** Begin your code here **/
      
       /** end your code here **/
   }
  
   /** Paste addToEnd() method here **/
   public void addToEnd(int data) {
       /** Begin your code here **/
       if(head == null)
       {
       head = new Node(data, head);
       }
       else
       {
       Node position = head;
       while(position.link != null)
       {
       position = position.link;
       }
       position.link = new Node(data, null);
       }
       /** end your code here **/
   }

   public void printList(){
       Node position = head;
       if (head == null)
           System.out.println("The list is empty.");
       while(position != null){
           System.out.print(position.data + " ");
           position = position.link;
       }
       System.out.println();
   }
  
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       LinkedList list = new LinkedList();
      
       System.out.println("How many values do you want to add to the list?");
       int toAdd = input.nextInt();
       for(int i = 0; i < toAdd; i++) {
           System.out.println("Enter value " + (i + 1));
           list.addToEnd(input.nextInt());
       }
       System.out.println("Here is the list");
       list.printList();
      
       System.out.println("The sum of all of the elements in the list is " + list.sum());
      
       input.close();
   }
}

Explanation / Answer

import java.util.Scanner;

/**
* Question:
*
* Finish the implementation of the sum() method. This method will return the
* sum of all of the elements in the list, or 0 if the list is empty.
*
* For instance, given the list 1 2 3 sum() would return 6.
*
* You will also need to add the "addToEnd()" method that you wrote in question
* 5. You can simply copy and paste that into this class.
*
* Only add code in between the comments that say "Begin your code here" and
* "End your code here"
*
* or immediately after "Paste addToEnd() method here"
*/
public class LinkedList {
   // Private inner class Node

   private class Node {
       int data;
       Node link;

       @SuppressWarnings("unused")
       public Node() {
           data = Integer.MIN_VALUE;
           link = null;
       }

       public Node(int x, Node p) {
           data = x;
           link = p;
       }
   }

   // End of Node class

   public Node head;

   public LinkedList() {
       head = null;
   }

   /**
   *
   * @return
   */
   public int sum() {
       /** Begin your code here **/
       int sum = 0;
       Node position = head;
       if (head == null)
           return sum;
       while (position != null) {
           sum += position.data;
           position = position.link;
       }

       return sum;
       /** end your code here **/
   }

   /** Paste addToEnd() method here **/
   public void addToEnd(int data) {
       /** Begin your code here **/
       if (head == null) {
           head = new Node(data, head);
       } else {
           Node position = head;
           while (position.link != null) {
               position = position.link;
           }
           position.link = new Node(data, null);
       }
       /** end your code here **/
   }

   public void printList() {
       Node position = head;
       if (head == null)
           System.out.println("The list is empty.");
       while (position != null) {
           System.out.print(position.data + " ");
           position = position.link;
       }
       System.out.println();
   }

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       LinkedList list = new LinkedList();

       System.out.println("How many values do you want to add to the list?");
       int toAdd = input.nextInt();
       for (int i = 0; i < toAdd; i++) {
           System.out.println("Enter value " + (i + 1));
           list.addToEnd(input.nextInt());
       }
       System.out.println("Here is the list");
       list.printList();

       System.out.println("The sum of all of the elements in the list is "
               + list.sum());

       input.close();
   }
}

OUTPUT:

How many values do you want to add to the list?
3
Enter value 1
1
Enter value 2
2
Enter value 3
3
Here is the list
1 2 3
The sum of all of the elements in the list is 6