Question: JAVA question create a class, Sum that extends an ArrayList of Integer by adding a public method, int sum() that returns the sum of all

JAVA question

create a class, Sum that extends an ArrayList of Integer by adding a public method, int sum() that returns the sum of all elements in the list of Integers (add the elements up each time the method is called).

Second, once you have the above working properly, enhance the solution such that the sum is stored and maintained as a public instance field of Sum (the field is public ). Instead of recalculating the total each time the sum() method is called, it should just be able to return the value of the sum field. To do this, you'll need to adjust the value of your sum field each time the contents of the array list change. Note that in order to do this correctly, many of the standard methods of the ArrayList class must be overwritten so that they are able to adjust the sum as elements are added or removed from the list. For this question you need only override the four 'add' methods:

boolean add(Integer i),

void add(int index, Integer i),

boolean addAll(Collection c), and

boolean addAll(int index, Collection c).

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 Databases Questions!