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

Java assignment How to use recursive method to do this? “fix” the list based on

ID: 3842605 • Letter: J

Question

Java assignment

How to use recursive method to do this?

“fix” the list based on the following rules.

  

      a. if list is null, returns

      b. if total of list is negative, list should become a list with a single item, 100.0

      c. if total of list is less than 100, add a single item with the short-fall at the beginning of the list. for example, if list = [30, 60], list should become [10, 30, 60]

      d. if total of list is more than 100, remove the first item and make a recursive call to fix.For example, if list = [40, 30, 80], list should become [20, 80]

   you CAN write a non-recursive solution for this in order to move on to the next stage, however you won't get marks for this method if the solution is not recursive

public static void fix(ArrayList<Double> list) {to be completed }

Explanation / Answer

public static void fix(ArrayList<Double> list) {

       if (list == null)

           return;

       else if (list.size() < 0) {

           list.add(100.0);

       } else if (list.size() < 100) {

           list.add(0, 10.0);

       } else if(list.size() > 100){

           list.remove(0);

           fix(list);

       }

   }

=============================================
I have tested the function as well, It works fine.
Test Code :

   ArrayList<Double> list = new ArrayList<Double>();

       for(int i=0;i<103;++i){

           list.add(1.0 * i);

       }

       fix(list);

       for(int i=0;i<list.size();++i){

           System.out.print(list.get(i) + " ");

       }



Thanks

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