Submit this homework using Gradescope. You can type up your answers, or write legibly and scan them. Do not attempt to submit a paper copy in class, as it will not be accepted.
(1 point) Write a method that computes the sum of the elements in an array of
int
s, with signaturepublic int sum(int[] a)
.(1 point) Now, write a method that computes the sum of the elements in a
List<Integer>
, with signaturepublic int sum(List<Integer> l)
. You may assume theList
class has been imported.(2 points) Write a method
public void insert(int x, int[] a)
that inserts elementx
at the first cell in the array, moving everything else out of the way by shifting it “down” one element. The last element will just be dropped. For example, suppose you had an arraya
ofint
s of length 4 that contained the following values:[10, 2, 18, 0]
. After a call ofinsert(42, a)
, the new contents of the array should be[42, 10, 2, 18]
.(1 point) How would you accomplish a similar task with a
List<Integer> l
? That is, what single line of code would let you insert an element at the start of the listl
? (Unlike the array method above, this will not lose the last element of the list.)