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

Really need help with this (Java), thanks! b. Method filterRange: accepts an Arr

ID: 3794431 • Letter: R

Question

Really need help with this (Java), thanks!

b. Method filterRange: accepts an ArrayList of integers and two integer values min and max as parameters and removes all elements whose values are in the range min through max (inclusive) from the list. For example, if a variable called list stores the values: [1, 4, 7, 9, 2, 7, 7, 5, 3, 5, 7, 8, 6, 7] The call of filterRange(list, 5, 7) should remove all values between 5 and 7, therefore it should change the list to store [1,4, 9, 2, 3, 8]. You may assume that the list is not null. After the call print the list.

Explanation / Answer

Tested on Eclipse

/********************************Filter.java********************/

import java.util.ArrayList;
/**
*
* @author lmali
*
*/
public class Filter {
   /**
   *
   * @param ArrayList
   * al
   * @param int
   * min
   * @param int
   * max
   * @return ArrayList
   */
   public static ArrayList<Integer> filterRange(ArrayList<Integer> al, int min, int max) {

       ArrayList<Integer> temp = new ArrayList<Integer>();
       for (int i = 0; i < al.size(); i++) {
           /**
           * If value at index i in al is less then min or greater the max
           * then add into temp ArrayList
           */
           if (al.get(i) < min || al.get(i) > max) {
               temp.add(al.get(i));
           }
       }
       /* Returning temp ArrayList */
       return temp;
   }

   /**
   * Printing ArrayList
   *
   * @param ArrayList al
   */
   public static void printList(ArrayList<Integer> al) {

       System.out.println(al);
   }

   public static void main(String[] args) {

       ArrayList<Integer> list = new ArrayList<>();
       // 1, 4, 7, 9, 2, 7, 7, 5, 3, 5, 7, 8, 6, 7
       list.add(1);
       list.add(4);
       list.add(7);
       list.add(9);
       list.add(2);
       list.add(7);
       list.add(7);
       list.add(5);
       list.add(3);
       list.add(5);
       list.add(7);
       list.add(8);
       list.add(6);
       list.add(7);
       /**
       * Calling filterRange method
       */
       ArrayList<Integer> temp = Filter.filterRange(list, 5, 7);
       /**
       * calling printList method
       */
       Filter.printList(temp);

   }

}

/****************************output************************/

[1, 4, 9, 2, 3, 8]

Thanks a lot