Question: Addition with ListsGiven a List of Integer called numbers passed as a parameter into the adding ( ) method, return the List with each element

Addition with ListsGiven a List of Integer called numbers passed as a parameter into the adding() method, return the List with each element incremented by 2.
Iterate through each element in the numbers List.
For each element, add 2 to its value.
Update the element in the numbersList with the new value (incremented by 2).
After processing all elements, return the modified numbers List.
import java.io.*;
import java.util.*;
import java.math.*;
public class CodingQuestion {
static List adding(List numbers){
/***** DO NOT CHANGE THE CODE ABOVE THIS LINE *****/
// WRITE YOUR CODE HERE
}// end of adding()
/***** DO NOT CHANGE THE CODE BELOW THIS LINE *****/
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num1= in.nextInt();
int num2= in.nextInt();
int num3= in.nextInt();
int num4= in.nextInt();
int num5= in.nextInt();
List numbers = new ArrayList<>();
numbers.add(num1);
numbers.add(num2);
numbers.add(num3);
numbers.add(num4);
numbers.add(num5);
System.out.println(adding(numbers));
}// end of main()
}
STDOUT
Expected STDOUT
[12,22,32,42,52]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!