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

Write a method(in java)which takes an ArrayList of integers as input and compute

ID: 3773831 • Letter: W

Question

Write a method(in java)which takes an ArrayList of integers as input and computes a new ArrayList whose 0th entry is 1, whose ith entry is the sum of the i1st entry and ith entry of the input ArrayList for 1 i length of input ArrayList, and whose last entry is 1. So if [2, 3, 4] is the input, the new ArrayList should be [1,5,7,1]. This method should return the new ArrayList. Write you main program to read of sequence of values into an ArrayList and ask the user for the number of times to iterate the method. Your main program should then call the above method as many times as the user requested, printing each returned ArrayList. For example, if the user enters the sequences 1 2 3, and asks for 5 iterations, your program should print 123 1351 14861 1 5 12 14 7 1 1 6 17 26 21 8 1 1 7 23 43 47 29 9 1

Explanation / Answer

import java.util.*;

import java.lang.*;

import java.io.*;

class SumArrayList
{
public static void main (String[] args) throws java.lang.Exception
{
  
ArrayList<Integer> numbers = new ArrayList<Integer>();

ArrayList<Integer> newnumbers = new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the arraylist");

while(scanner.hasNextInt()) {
int i = scanner.nextInt();
numbers.add(i);
}

System.out.println("Please enter the number of times to iterate through the method");
int k=scanner.nextInt();
SumArrayListMethod(numbers,k);

}

/*Method which prints the sum of i-1 and ith element of the arrayList*/   

public static void SumArrayListMethod(ArrayList<Integer> A,int k)
{
ArrayList<Integer> B= new ArrayList<Integer>();
if(k>0){

B.add(1);
for(int j=1;j<=A.size()-1;j++)
{
B.add(A.get(j)+A.get(j-1));
}
B.add(1);
for(Integer n : B)
System.out.print(n+" ");

}
k=k-1;
SumMethod(B,k);


}
}

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