Question

In: Computer Science

- Write a method sum that expects a List<Integer> as a parameter. The method returns an...

- Write a method sum that expects a List<Integer> as a parameter. The method returns an int representing the sum of the integers in the list.

- Write an index-based loop that prints the contents of a list.

Solutions

Expert Solution

Java Program:

import java.util.*;

//Class that holds list of integers
public class ListIntegers
{
   //Main method
   public static void main(String[] args)
   {
       //List of integers
       List<Integer> integers = new ArrayList<Integer>();
      
       //Adding element in the list
       integers.add(23);
       integers.add(6);
       integers.add(18);    
       integers.add(4);    
       integers.add(14);
      
       System.out.println("\n\n Elements present in the list: \n");
       //Printing elements in the list
       print(integers);
      
       //Calculating sum of elements in the list
       int sumOfIntegers = sum(integers);
      
       //Printing sum
       System.out.println("\n\n Sum of elements in the list: " + sumOfIntegers + " \n");
   }
  
   //Method that prints the elements in the list using index-based loop
   public static void print(List<Integer> integers)
   {
       //Iterating over list
       for(int i=0; i<integers.size(); i++)
       {
           //Printing element
           System.out.print( " \t " + integers.get(i) );
       }
   }
  
   //Method that finds the sum of elements in the list
   public static int sum(List<Integer> integers)
   {
       int sum = 0;
      
       //Iterating over list
       for(int i=0; i<integers.size(); i++)
       {
           //Accumulating sum
           sum += integers.get(i);
       }
      
       //Return the sum of elements in the list
       return sum;
   }
}

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

Sample Output:


Related Solutions

Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the...
Write, in Java, a recursive method countBinaryStrings that has one integer parameter n and returns the number of binary strings of length n that do not have two consecutive 0’s. For example, for n = 4, the number of binary strings of length 4 that do not contain two consecutive 0’s is 8: 1111, 1110, 1101, 1011, 1010, 0111, 0110, 0101. For this problem, your method needs to return only the number of such strings, not the strings themselves. You...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of...
Create a method that takes a HashMap<Integer, Integer> and returns the sum of the keys of the HashMap.
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list....
write the method “getMaxValue” that finds and returns the maximum value in an integer linked list. If the list is empty, then it should return 0. use the provided code below public class Question03 { public class ListNode//public for testing purposes { public int data;//public for testing purposes public ListNode link;//public for testing purposes public ListNode(int aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//public for testing purposes public int getMaxValue() { //----------------------------------------------------------------------------------- //Write...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will...
Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a method that takes an integer array as its parameter and sorts the contents of...
Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm. Call this method after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed. CODE SO FAR: import java.util.*; public class ArrayInteger { public static void getRandomNumber(int A[],int n){ Random...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT