Ask Question
6 April, 13:18

g Create a package named lab7 (no spaces, ALL in lowercase) where you will place your lab files. Write a class FilterWith with a method called filterRange that 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). For example, if a variable called list stores the values [4, 7, 9, 2, 7, 7, 5, 3, 5, 1, 7, 8, 6, 7], the call of filterRange (list, 5, 7); should remove all values between 5 and 7, changing the list to store [4, 9, 2, 3, 1, 8]. If no elements in range min-max are found in the list, or if the list is initially empty, the list's contents are unchanged. Create an application that creates an object of FilterWith and invokes the filterRange method and show the results

+1
Answers (1)
  1. 6 April, 15:46
    0
    See explaination

    Explanation:

    import java. util. ArrayList;

    import java. util. Scanner;

    public class ListFilter

    {

    public static void filterRange (ArrayList list, int min, int max)

    {

    ArrayList listNew=new ArrayList ();

    for (int i=0; i
    {

    if (list. get (i) >=min && list. get (i) <=max) / /Checking if element is between min and max

    {

    listNew. add (list. get (i));

    }

    }

    list. removeAll (listNew); / /removing all elements from list.

    }

    public static void main (String[] args)

    {

    ArrayList list=new ArrayList ();

    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 (1);

    list. add (7);

    list. add (8);

    list. add (6);

    list. add (7);

    Scanner sc=new Scanner (System. in);

    System. out. println ("Enter min : ");

    int min=sc. nextInt ();

    System. out. println ("Enter Max : ");

    int max = sc. nextInt ();

    filterRange (list, min, max);

    //Displaying new List

    for (int i=0; i
    {

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

    }

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “g Create a package named lab7 (no spaces, ALL in lowercase) where you will place your lab files. Write a class FilterWith with a method ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers